• 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 using writeObject() & readObject() method to add Transient variable

 
Ranch Hand
Posts: 1087
Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


I have Serialization query using writeObject() & readObject() method to add Transient variable, but its giving null pointer exception for "after: collar size is please advice where I am going wrong.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:Hi,

I have Serialization query using writeObject() & readObject() method to add Transient variable, but its giving null pointer exception for "after: collar size is please advice where I am going wrong.


Transient variable cant be serialize ..ok and when you deserialize your dog object at that time collers value is null.so nullpointerexception is ....
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Haresh Kumar wrote:Transient variable cant be serialize ..


Haresh,

1. Welcome to JavaRanch.
2. You don't need to quote OP's entire post when you reply; it makes threads incredibly long. Just include the relevant portion.
3. Your reply should go outside the 'quote' block.

I've corrected yours this time.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:I have Serialization query using writeObject() & readObject() method to add Transient variable, but its giving null pointer exception for "after: collar size is please advice where I am going wrong.


Vishal, could you please show us the entire error message exactly as you get it; otherwise we're just guessing.

One thing I did notice: Dog.readObject() should be private.

Also: those try...catch blocks of yours may be causing problems, since they don't halt execution. With something like serialization, it's much better to just let the program fail. Indeed, until you're sure that everything's working, I wouldn't use any try...catch blocks.

Winston
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Vishal Hegde wrote:I have Serialization query using writeObject() & readObject() method to add Transient variable, but its giving null pointer exception for "after: collar size is please advice where I am going wrong.


Vishal, could you please show us the entire error message exactly as you get it; otherwise we're just guessing.

One thing I did notice: Dog.readObject() should be private.

Also: those try...catch blocks of yours may be causing problems, since they don't halt execution. With something like serialization, it's much better to just let the program fail. Indeed, until you're sure that everything's working, I wouldn't use any try...catch blocks.

Winston





I am getting the below exception but I am using writeObject() & readObject() so that it will even take the state of transient value at a time, still I am getting NULL Pointer Exception.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:
I am getting the below exception but I am using writeObject() & readObject() so that it will even take the state of transient value at a time, still I am getting NULL Pointer Exception.



1. Make writeObject() and readObject() private as Winston suggested.

2. Add a println() to make sure that d is not null, and that it's getCollar() that's returning null. I know it seems obvious that this must be the case, but we must always check our assumptions to eliminate paths of investigation.

3. Add println() calls to writeObject() and readObject() so you can see what they're doing.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I missed it.

Exactly where in that code are you providing a special initialization of the Collar reference "theCollar"?

As a transient reference you expect that the value should be null just like a transient int will be 0 on deserialization.

There are plenty of tutorials - here is a recent one.

Bill


 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill, theCollar is being set in the readObject method.

For everyone: Winston's first comment nails the issue. The readObject method needs to be private, as specified in the API for java.util.Serializable When you make it public, it doesn't get called. No, it makes no sense by any modern understanding of how Java should work. But that's how serialization was implemented in Java, back in the dawn of time.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I changed the the method to private and now getting the result thnks
 
reply
    Bookmark Topic Watch Topic
  • New Topic