• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization problem  RSS feed

 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone.
I have made this simple program on Serialization


When it runs it throws an Exception however i want it to give the values of x,y and z in output.Please suggest a way .I don't want to make class B serializable as i want to see the working of transient variables. I will be really thankful of u.Reply soon.




 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reformatted your code for better reading. I also already had an A class in my project so I changed yours to Ax. A couple of notes:
1. Don't just catch exceptions and eat them. They tell you what the problem is. I printed the stack trace for the offending Exception. When you run the code it points to the problem line.
2. The problem is the getValue() method. In it you assume you have an instance of B and try to get the b.z value. But the b instance is null because it is declared transient. I changed that method too so that it does not throw the exception.
3. you should also always close your streams.
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tom for the reply but the problem is that the value of z in output is null.That means the instance variable b that refers to class B is not restored that is however we tried to use writeInt and readInt methods to manually do the serialization but it didn't work.What do you have to say about that?
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as i want to see the working of transient variables
Well, you now see how transient variables work. They do not get set when you deserialize. Perhaps you're thinking of the interface java.io.Externalizable?
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But we can serialize and deserialize them manually.Isn't that true.Infact i have written the code just to do that.


TRANSIENT VARIABLES CAN BE SERIALIZED
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited my previous reply to add the following while you were responding.
Perhaps you're thinking of the interface java.io.Externalizable?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!