• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization query

 
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, so the compiler doesn't check for null pointers when it deserializes?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic