Hello: I have a solution which uses serialization to cache data to disk. Code works fine under JDK 1.3 but not under JDK 1.4. A set containing a list of objects are serialized and deserialized. Under 1.4 several times serialization/deserialization works and then a null pointer exception is thrown which is mapped to an object being null in the set at deserialization level. If I deserialize the cache with 1.3 code then it works but not with 1.4 code. Did browse the difference in serialization from 1.3 to 1.4 but nothing applies to my case. Any ideas?
How does the code of the serializable class look like?
Joined: Nov 25, 2003
I am serializing a HashSet which contains a list of TopClass. TopClass contains an internal EventClass. EventClass contains an EventElement1Class, EventElement2Class, and EventElement3Class. Each of element classes in turn either extend or contain other classes. Basically, I am serializing a tree of classes with root being the TopClass. Edit 1: After doing some research I found out that the problem lies with hashcode method being defined in TopClass. If I remove this method (use Object's hashcode) then everything is fine. Otherwise, a null pointer is returned. The problem is that Object's hashcode returns a number based on the reference and not content. My hashcode returns a number based on name attribute of EventClass. Is this a bug with JDK1.4 or am I doing something wrong? [ November 26, 2003: Message edited by: Mehrshad Setayesh ] Edit 2: My TopClass was an inner class inside the serializer/deserializer class. By chance I decided to define it in its own java file and it worked with its own hashcode() method. I do believe this is a bug in JDK 1.4 because of the fact mentioned above. The bug description would be: in case of a complicated graph with root class being an inner class of serializer/deserializer class and with root class including a hashcode method a null pointer exception is thrown when deserializing. [ November 26, 2003: Message edited by: Mehrshad Setayesh ]
Joined: Oct 08, 2001
I am serializing a HashSet which contains a list of TopClass. ... After doing some research I found out that the problem lies with hashcode method being defined in TopClass. If I remove this method (use Object's hashcode) then everything is fine. Does TopClass also overrides equals()? Since it is part of a hash-based collection, it must fulfill the contract: any two instances of TopClass that are considered equal must return the same hashcode. Are you sure that you are in compliance with that contract? My TopClass was an inner class inside the serializer/deserializer class. Is TopClass a static member class? If so, you are OK. Otherwise, you may be interested in this, -- serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged by Sun. Another thing: instead of serializing a HashSet, wrap it with another class and explicitely declare the serialVersionUID field in that wrapper class. Does that work?