• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization query  RSS feed

 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I was just dabbling in the serialization code given in K. Sierra book on SCJP.

I ran the following code snippet



Now according to the book, as the collar class has not implemented serializable interface, to bypass that I created a transient object of type collar in dog class. According to the book, such an action will compile the code, but at runtime will throw a NullPointerException as the state of the 'thecollar' object wasn't saved. Now I know I'm missing something in the code, but can't get hold of it.. probably just too tired today.

I hope someone can point out. And I'm just a newbie in java, So please don't tell me how stupid question it was --> I already know that
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since transient object can not be serialized. that object wont save as binary in to file.
when you deserialize the object(dog) you dont get the object which stored in c filed of dog. so JVM assign null to it.

note: you dont get any exception till now.
but when you call any method on c here after obviously you get NPE.
 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, so the compiler doesn't check for null pointers when it deserializes?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is not involved at all when your program runs. So the compiler doesn't have anything to do with deserialization.
 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the code to following, and am assigning the value of instance variable of c which is of type collar to an integer in the code, the code still works fine:

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, we can’t read that code since you didn’t indent it.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. *you are customizing default serialization* using readObject/writeObject.
 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@campbell ritchie: I'm just learning, indenting my code is the last thing on my mind.. no offense.

@seetharaman: yes i know that, please read my question and answer the query please.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, I don't see a question. Or rather, I see a set of posts which contain various statements which might be questions which might still be unanswered. So, could you summarize the action so far and ask a clear question? And since there's various code in the thread now, specify which code you're talking about?

(By the way the easiest way to post indented code is to copy the indented code from your text editor and paste it here, inside the code tags.)
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiveen Pandita wrote:@campbell ritchie: I'm just learning
Indentation is part of what you have to learn.
indenting my code is the last thing on my mind.. no offense.

@seetharaman: yes i know that, please read my question and answer the query please.
If people can’t read your code because you haven’t formatted it correctly, how can you expect them to answer your question?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!