• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deserialize object from file failing

 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm storing an object to file by declaring a static serialVersionUID and using an ObjectStreamField[] to declare the fields that I want to store when my object gets serialized. I have my readObject() and writeObject() methods declared. One of my fields is an array of custom objects(DataPoints). As long as that datapoint array has a length of zero (no objects assigned to it), my serializing and deserializing works fine. However, as soon as the array has any elements in it, reconstituting it from a file fails. It claims my master object is null coming out of the file. I have gone through multiple tests and checked my DataPoint object to be sure that it is coded correctly for serialization and I can't find anything wrong with it.

I'v narrowed down an odd occurance that may indicate something. In my readObject() method I use

This makes the fields I declared for the object readable when the object is reconstituted from the file. When the DataPoint array is empty, a println() on the very next line prints out just fine. However, when the array isn't empty, the println() never gets executed. OK, error maybe, but when I added the generic catch(Exception e) to the code, it would never get activated. I tacked on a finally{} just for curiosity's sake and it executed.

What could get activated from inside a method call (stream.readFields()) that could break out of executing the very next line, throw no error, but still execute a finally{} ?

I'm stumped. I am using java 1.5. Is there anything in the new version that could be causing this? I'll have to go check out it out.

Any help/guidance/questions you guys can provide/ask are welcome. I'm just looking for something to spark an idea or provide a solution path I can take.

Thanks,
Chris
[ January 17, 2006: Message edited by: Chris Shepherd ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Chris.

Are you sure that the objects you are storing into the array are also serializable?

Rememeber also that altough an object is serializable not necesarily its ancestors are, hence, the serialization/deserialization process would just write/read the serializable state of the object and would set to its default values all the object members that were inherited from non-serializable ancestors.

Take a look a this example. In this Mamal is not serializable, but Lion is. When I serialize Lion the state of Mammal is nevere serialized, so when I read the object back from the stream I cannot tell what the value of the member "name" was, because name is on a non-serializable ancestor of Lion, hence it was not written into the stream.



One must be careful with arrays and collections, because the fact that the collection or the array is serializable does not imply that the contents of them are serializable.

This sounds a bit like the problem you describe. If it is maybe you could do some custom serialization by means of the writeObject and readObject methods.

Regards,
Edwin Dalorzo
[ January 17, 2006: Message edited by: Edwin Dalorzo ]
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions Edwin. I took your advice and made sure I had everything serializable down the line, and it was all correct. I appreciate your thoughts.

I did find the problem tho. After I double checked all the classes for Serializable again,I wrote in some test code to serialize out just the problem object. I was able to pinpoint 4 problems in my readObject() method that were all related. This was my initial code. vectChngAmp is a float and vectChngTime is a long.

Evidently in fields.get(), java doesn't adapt the 0 to whatever data type is expected like it does in standard assignments and such. Here is what actually worked

Seems kind of odd to me that it doesn't work like the normal assignment does since that 0 value is meant to be assigned to the variable if the field doesn't exist.

Ah well, another lesson learned. Hope this helps somebody later on.

Chris
[ January 18, 2006: Message edited by: Chris Shepherd ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!