-4

why the code is un deserlaizable ! cannot read serlaizable file ! it write correctly in file but cannot read it

enter image description here

public ArrayList<customer> read_file(){
ArrayList <customer>file = new ArrayList<customer>();
try {
    File f= new File("file.bin");
    ObjectInputStream is= new ObjectInputStream (new FileInputStream("file.bin"));
    int x=(int) f.length();


    while(is.available()!=0){
    file.add((customer)is.readObject()) ;

    }
} catch (FileNotFoundException ex) {
    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException | ClassNotFoundException ex) {
    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}

return file ;}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Paste code here. Don't give some external link. – santosh-patil Apr 17 '17 at 17:12
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Apr 17 '17 at 17:32
  • I recommendation, don't use serialization, instead use something like JAXB or some other method, serialization is only meant for the short term storage of an object, typically for transmission over the wire ... it has problems and is very easily broken – MadProgrammer Apr 17 '17 at 22:28

1 Answers1

0

You error is null pointer exception. You initialized your customer array as null. I would suggest using an ArrayList instead.

user5806607
  • 165
  • 1
  • 2
  • 11