Bookmark Topic Watch Topic
  • New Topic

Issue reading a serialized object using ObjectInputStream

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi All,

I am writing a dummy class to see how ObjectInputStream works. I was able to successfully serialize an Object, but have issue reading it.

Here is my Address Object class:




Class used to serialize object:





Class used to read serialized object from a file:




When i run my Deserializer class i am seeing this error:




Issue is with this line in the Deserializer class: address = (ArrayList<Address>) ois.readObject();


What can be the issue here?

Thanks
Adi
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Report post to moderator
The problem is you are writing the Address objects out one object at a time and then reading them in as if they are in an ArrayList. You either need to write out the ArrayList object (which will serialize each of the Address object stored in it) or you need to read in each individual Address object. Personally I'd go with writing out the ArrayList object because if you write out individual objects when you come to read them back in you need to know exactly how many objects to read in.
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Report post to moderator
Locking this thread as it's a duplicate of http://www.coderanch.com/t/614918/java/java/reading-serialized-object-ObjectInputStream
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!