Sunday, September 25, 2011

Class Deserializing issue with a class containing ArrayList as member variable


In one of my program, I had a class declared with a Member Variable whose datatype is ArrayList.

I serialized and saved the class contents in the text file.

When I tried to deserialize, I got back only the first value in the ArrayList, not all the values. Even though the file had more than 1 value, always I was getting back only one value into my class member variable.

After several debugging I thought of adding a constructor to the class and also inside that class constructor I initialized the ArrayList member variable to new ArrayList. After doing this then the deserialization happened properly. Then I got all my values back into my ArrayList member variable.

Whenever deserialization occurs the class constructor is called and proper initialization happens and then the ArrayList is being filled.

Hope this tip saves time for someone !


All Blogs so far ...