• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization: Non serialized class issue

 
Ajit Sawant
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
---

---

[ December 28, 2008: Message edited by: Ajit Sawant ]

[ December 28, 2008: Message edited by: Ajit Sawant ]
[ December 29, 2008: Message edited by: Ajit Sawant ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) For "System.out.println("After Deserialization collarSize value :" + d.getCollar().getCollarSize());"
The output is 0, expecting it to be 5
2) For "System.out.println("After Deserialization dogSize value :" + d.dogSize);" it gives error "java.lang.NullPointerException at SerializeCat2.main(SerializeCat2.java:97)

Assuming that you wrote the answers of these question in wrong order


About the question 1
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;
}

About question 2,
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.
Hope now you understand the execption.

About question 3,

The idea of using the temp is to store the value of a non-serializble variable collarSize;
Avoid the use of temp is not a problem , but that would create an unrequried method calling....


Hope i am clear and correct.
 
Sunil Bangia
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you require sun java voucher mail us............... sunil.bangia@niit.com


 
Ajit Sawant
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
Ajit Sawant
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Then tried to print the values
System.out.println("After Deserialization dogSize value :" + d.dogSize);
System.out.println("After Deserialization new Collar's collarSize: " + c.getCollarSize());
System.out.println("After Deserialization collarSize value :" + d.getCollar().getCollarSize());

The output is
After Deserialization dogSize value : 5
After Deserialization new Collar's collarSize: 3
java.lang.NullPointerException at SerializeCat2.main(SerializeCat2.java:105)
[ December 30, 2008: Message edited by: Ajit Sawant ]
 
Ajit Sawant
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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))

3) No new Collar object creation ( Collar c = new Collar (is.readInt()) )

4)After deserialization statement d = (Dog)ois.readObject();
d.setCollar(c);

This works. It will also work if I do Collar c = new Collar (is.readInt())
in readObject(ObjectInputStream i).

In both cases is it a true deserialization? as I'm setting the value for "theCollar" after executing the deserialization d = (Dog)ois.readObject();
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic