Serialization: Non serialized class issue

Ajit Sawant

Ranch Hand

Posts: 33

posted 8 years ago

Hello, This is a modified version of K&B code (SCJP6 pg 466). Instead of putting serialization logic in the serializable class Dog, I have put that in a separate class. In the code below 1)For "System.out.println("After Deserialization dogSize value :" + d.dogSize);" The output is 0, expecting it to be 5 2)For "System.out.println("After Deserialization collarSize value :" + d.getCollar().getCollarSize());" it gives error "java.lang.NullPointerException at SerializeCat2.main(SerializeCat2.java:97) 3) Also, in the private method writeObject is there any way to avoid using a temp variable? Look like the object d is not getting deserialized. Please let me know why it is not working. Thanks in advance. Ajit ---

Hi James, I have corrected the question's order and message mismatch. Your repsonse

the output would have been 5, if you used the value that you passed to the method.What did you do with the size variable??? public Dog (Collar collar, int size){ theCollar =collar; }

Where to use the value to pass it to a method? I didn't understand you question/hint. I expect os.defaultWriteObject() to care of Dog object as it is Serializable.

Your reponse

When deserialized, the value of transient variable of a serializble class is always set to its default value. transient private Collar theCollar; So, when the d.getCollar() of d.getCollar().getCollarSize(); returns, it would be returning a null value.

As the Collar class is not serializable I'm witing it using os.writeInt(temp); //temp (has value 3) store the value for CollarSize. When I use Collar c = new Collar (is.readInt()). I expect it to read 3 create a new Collar object by using Collar c = new Collar (is.readInt()) in my code. Then build the the Dog object again Dogsize 5 and collarSize 3 (new Collar created durinf deserializtion. Please correct me if my understanding is wrong. Thanks Ajit

When your Are creating the Dog object here, Dog d = new Dog(c,5); You are sending two arguments, And in the Dog constructor....

public Dog (Collar collar, int size){ theCollar =collar; }

you took the argument into the parameter size.And you did not assign it to any variable(its wasted). It should be ussed like this. public Dog (Collar collar, int size){ theCollar =collar; dogSize=size; }

Then build the the Dog object again Dogsize 5 and collarSize 3 (new Collar created durinf deserializtion.

In this method you created a new collar object.Which is actually meant for the Dog object's instance variable thecollar. But nowhere in the code you have assigned this newly reconstructed collar object to the dog's instance variable thecollar

Hope you understood ..

SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !

Ajit Sawant

Ranch Hand

Posts: 33

posted 8 years ago

Now i did dogSize= size. But I'm able to deserialize Dog. Unlke the example in the book, I can not do d.theCollar = new Collar(is.readInt()). How do set theCollar as it not in the same class file. I created a new Collar object, created a new Dog object so that the variable "theCollar" should get set. But it is not working. Q) Why should I create a new Dog object? Deserialization shpuld do it for me. By creating a new Dog object I'm calling the Dog constructor which should nt be executed as per deserialation compept. Any it did not work? How can get the same Dog object I have the new Collar object now? Iwas expecting "theCollar" to set bet deserialization using the new Collar c. I did the following

I made few change to make it run without error. I'm not that is the right way as I doing what I expect deserialization to do for me. I think it is not true deserialization 1) created a new meth od in Dog public void setCollar(Collar collar) { theCollar =collar; } 2)No explicit writing of Collar (os.writeInt(temp))