3 Replies - 245 Views - Last Post: 19 May 2014 - 05:49 AM Rate Topic: -----

#1 italy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-November 13

got stuck with the ClassCastException

Posted 18 May 2014 - 10:56 AM

I'm doing a project in netbeans. I have to make a on-line bank with all the GUI. In the middle i got stuck with the classcastexception.. Any help would be appreciated... thanks in advance

public class Banca {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
       
        // dati dei clienti
        Anagrafica c = new Anagrafica();
        
        Dat d1 = new Dat(10,12,1990);  int codice1 = 1;
        Cliente c1 = new Cliente(codice1,"Mark","Bennet",d1,"mb10121990","piva1","via padova","20147","Milano");
        c.Inserimento(codice1, c1); 
        
        Dat d2 = new Dat(1,2,1980); int codice2 = 2;
        Cliente c2 = new Cliente(codice2,"Danny","Hayley",d2,"dh121980","piva2","via cesare","21529","Verona");
        c.Inserimento(codice2, c2);
        
        Dat d3 = new Dat(8,4,1975); int codice3 = 3;
        Cliente c3 = new Cliente(codice3,"Tom","Watson",d3,"tw841975","piva3","via Asiago","24851","Varese");
       c.Inserimento(codice3, c3); 
        
        // dati dei conti
        Conti con = new Conti();
        
        String[] in1 = {"Jonny","Romeo","Anne"}; int num1 = 1;
        Conto cont1=new Conto(codice1,num1,in1,1000.0,5);
        con.Inserimento(num1, cont1);
        
        String[] in2 = {"Tony","Watt","Grey"}; int num2 = 2;
        Conto cont2=new Conto(codice2,num2,in2,12000.0,9);
        con.Inserimento(num2, cont2);
        
        String[] in3 = {"Key","Sam","Bonny"}; int num3 = 3;
        Conto cont3=new Conto(codice3,num3,in3,42000.0,8);
        con.Inserimento(num3, cont3);
        
        //Dati dei movimenti 
        Movimenti m = new Movimenti();
        
        Dat da1 = new Dat(1,2,1875);
        Movimento m1 = new Movimento(1,da1,"Prelievo",100.0,num1,"causale1");
        m.Inserimento(m1);
        
        Dat da2 = new Dat(8,10,1900);
        Movimento m2 = new Movimento(2,da2,"Versamento",250.0,num1,"causale2");
        m.Inserimento(m2);
        
        Dat da3 = new Dat(8,11,1999);
        Movimento m3 = new Movimento(3,da3,"Prelievo",500.0,num1,"causale3");
        m.Inserimento(m3);
        
        Dat da4 = new Dat(18,12,1975);
        Movimento m4 = new Movimento(4,da4,"Prelievo",150.0,num2,"causale4");
        m.Inserimento(m4);
        
        Dat da5 = new Dat(27,2,1950);
        Movimento m5 = new Movimento(5,da5,"Versamento",550.0,num2,"causale5");
        m.Inserimento(m5);
        
        Dat da6 = new Dat(15,1,2010);
        Movimento m6 = new Movimento(6,da6,"Versamento",750.0,num3,"causale6");
        m.Inserimento(m6);
        
        Dat da7 = new Dat(18,9,2012);
        Movimento m7 = new Movimento(7,da7,"Versamento",7000.0,num3,"causale7");
        m.Inserimento(m7);
        
      /*  
       Home  h = new Home();
        h.setVisible(true); 
       */         
                
       
        
      File f = new File();
        
        //Scrittura dal file : 
      f.Salva(c, con, m);
      /*
       //Lettura dal file : Conti
           try{ 
            FileInputStream fN=new FileInputStream("file.txt");
            ObjectInputStream fIN=new ObjectInputStream(fN);
            
            try{
                Conti cy=(Conti) fIN.readObject();
                for (int k=1;k<= cy.size();k++)
                cy.Ricerca(k);
               
                
            }
            catch(EOFException e)
            { System.out.println("Eccezione 1: " + e.getMessage());}
        }catch(IOException | ClassNotFoundException e)
        {  System.out.println("Eccezione 2: " + e.getMessage());
        }
      */   
      //Lettura dal file : Conti
           
           try{ 
            FileInputStream fI=new FileInputStream("file.txt");
            ObjectInputStream fIN=new ObjectInputStream(fI);
            
              while (true)
               {
                 try{
                      Conti cn=(Conti) fIN.readObject();
                      System.out.println(cn);
                      
                   }
                 catch(EOFException e)
                 { System.out.println("Eccezione 3: " + e.getMessage());}
               }  
           }catch(EOFException e)
            { System.out.println("Eccezione 4: " + e.getMessage());}
           
       /*   
      //Lettura dal file: movimenti 
        try{ 
            FileInputStream f3=new FileInputStream("file.txt");
            ObjectInputStream fIN=new ObjectInputStream(f3);
            
            try{
                Anagrafica mn =(Anagrafica) fIN.readObject();
                 
                        System.out.println(mn);
                      
             }
            catch(EOFException e)
            { System.out.println("Eccezione 10: " + e.getMessage());}
                
            
        }catch(IOException | ClassNotFoundException e)
        { System.out.println("Eccezione 3: " + e.getMessage());}
          
        
    
       
           
          
       
    /*      
        
   
        try{
            FileOutputStream f = new FileOutputStream("file.txt");
            ObjectOutputStream fOut = new ObjectOutputStream(f);
            
            fOut.writeObject(c1);
            fOut.writeObject(c2);
            fOut.writeObject(c3);
            fOut.flush();
            fOut.close();
        } catch(Exception e)
        { System.out.println("Error1 : "+ e.getMessage());
        }
       
        Cliente client;
         try
         {  FileInputStream f2 = new FileInputStream("file.txt");
            ObjectInputStream fIn = new ObjectInputStream(f2);
             while (true)
             {   try
                 {  client = (Cliente) fIn.readObject();
                    System.out.println(client);
                 }
                 catch (EOFException e)
                 { break; }
             }
             f2.close();
         } catch (Exception e)
         { System.out.println("errore2:" + e.getMessage()); }
     */
  }}



This is my main class...Don't mind about the comments.. I was just testing the program...

public class Conti implements Serializable{
      TreeMap<Integer,Conto> conti = new TreeMap<Integer,Conto>();
     Conto c = new Conto();
     Movimenti m = new Movimenti();
     
      public void Inserimento(int numero, Conto c)
    { Integer num=numero;
      conti.put(num,c); }
    
    public void Cancellazione(int numero)
    { Integer num = numero;
      conti.remove(num); }
    
    public void Prelievo(int numero,double q)
    { Integer num = numero;
      c = (Conto) conti.get(num);
      double s = c.getSaldo();
      s -= q;
      System.out.println("euro"+q+" preliavato");
      System.out.println("saldo:"+s);
    }
    
    public void Versamento(int numero,double q)
    { Integer num = numero;
      c = (Conto) conti.get(num);
      double s = c.getSaldo();
      s += q;
      System.out.println("saldo:"+s);
    }
    
    public void Ricerca(int codice)
    { Integer codiceID = codice;
      System.out.println(conti.get(codiceID)); 
    }
    
    public int size()
    { int i=conti.size();
      return i;
    }
    
    
    public boolean controlloChiave(int numero)
    { Integer num=numero;
      if(conti.containsKey(num)) 
      { return true;}
      else  return false;
    } 
    
    
    public Conto Visualizza(Conto c)
    { return c;
    }
}


And this is the collection of the Bankaccounts
public class Movimenti implements Serializable {
    private TreeSet<Movimento> Transazioni=new TreeSet<Movimento>();
    
    
    public void Inserimento(Movimento m)
    { Transazioni.add(m);}
    
    
    public void Visualizza()
    { Iterator i = Transazioni.iterator();
      
      while (i.hasNext()) 
      {  System.out.print(i.next() + " "); }
      System.out.println();
      
    }
    
    public void ricerca(Conto c)
    { Iterator i = Transazioni.iterator();
      int numConto = c.getNumero();
      
      while (i.hasNext()) 
      {  Movimento m = (Movimento)i.next();
         if (numConto == m.getNumero())
         { System.out.print(m);}  
      }
    }
    
    public Movimento primi()
    { return (Transazioni.first());
    }
    
     public Movimento ultimo()
    { return (Transazioni.last());
    }
    
     public int size()
    { int i=Transazioni.size();
      return i;
    }
}

And here's the class of the activities of an account.. I'm not posting my classes Conto( Bankaccount) & Movimento (Activity) because those two already do what they should..

And here I add the exception error:

banca.Conti@1989f84
Exception in thread "main" java.lang.ClassCastException: banca.Movimenti cannot be cast to banca.Conti
at banca.Banca.main(Banca.java:108)
Java Result: 1


Once Again I'm thanking in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: got stuck with the ClassCastException

#2 italy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-November 13

Re: got stuck with the ClassCastException

Posted 18 May 2014 - 11:12 AM

Someone please help me!!! :( :( :( :( :( :(
Was This Post Helpful? 0
  • +
  • -

#3 Zavael  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 13-May 14

Re: got stuck with the ClassCastException

Posted 19 May 2014 - 05:12 AM

what is this doing?
f.Salva(c, con, m);


if saving, than maybe you should first read the Anagrafica object, then Conti and then Movimenti
Was This Post Helpful? 0
  • +
  • -

#4 italy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-November 13

Re: got stuck with the ClassCastException

Posted 19 May 2014 - 05:49 AM

thanks for the reply.. I found the mistake & repaired it...
I was writing all the three kinds of list on the same file.. that was the error...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1