开发者

using Object input\ output Streams with files and array list

开发者 https://www.devze.com 2023-01-01 06:55 出处:网络
I\'m an IT student, and it\'s time to finish my final project in Java. I\'ve faced many problems, this one I couldn\'t solve and I\'m really frustrated.

I'm an IT student, and it's time to finish my final project in Java. I've faced many problems, this one I couldn't solve and I'm really frustrated.

My code is lik开发者_Python百科e this:

in Admin class:

 public ArrayList cos_info = new ArrayList();
 public ArrayList cas_info = new ArrayList();
 public int cos_count = 0 ;
 public int cas_count = 0 ;


void coustmer_acount() throws FileNotFoundException, IOException{
String add=null;
 do{
    person p = new person() ;
    cos_info.add(cos_count, p);
    cos_count ++ ;
     add =JOptionPane.showInputDialog("Do you want to add more  coustmer..\n'y'foryes ..\n 'n'for No ..");
     }

   while(add.charAt(0) == 'Y'||add.charAt(0)=='y');
   writenew_cos();
  // add_acounts();
   }

    void writenew_cos() throws IOException{
    ObjectOutputStream aa = new ObjectOutputStream(new FileOutputStream("coustmer.txt"));
    aa.writeObject(cos_info);
    JOptionPane.showMessageDialog(null,"Added to file done sucessfuly..");
    aa.close();
     }

in Coustmer class:

void read_cos() throws IOException, ClassNotFoundException{
 person p1= null ;
 int array_count = 0;
 ObjectInputStream d = new ObjectInputStream(new FileInputStream("coustmer.txt"));
 JOptionPane.showMessageDialog(null,d.available() );
        for(int i = 0;d.available() == 0;i++){
        a.add(array_count,(ArrayList) d.readObject());
         array_count++;
        JOptionPane.showMessageDialog(null,"Haaaaai :D" );
        JOptionPane.showMessageDialog(null,array_count );
       }
       d.close();
       JOptionPane.showMessageDialog(null,array_count +"1111" );
             for(int i = 0 ; i<array_count&& found!= true ; i++){
            count ++ ;
            p1 =(person)a.get(i);
            user=p1.user;
            pass = p1.pass;
           // cos_checkpass();
                                    }
                        }

It just prints JOptionPane.showMessageDialog(null,d.available() ); and has an exception. Here a.add(array_count,(ArrayList) d.readObject()); p.s : person object from my own class and it's Serializable.


Pretty sure any class you write should not throw ClassNotFoundException


I would suggest putting a try/catch around your method call that throws the exception in the catch

catch(Exception ex){
ex.printStackTrace();
}

to find out where the error is coming from exactly, it might give you more information that what you're getting.

Also, I would recommend using

JOptionPane.showMessageDialog(null, "Add more Customers?", "Continue?",JOptionPane.ERROR_MESSAGE);

or something like that rather than getting the character of what the user typed, it creates a more user friendly interface.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号