• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization query using writeObject() & readObject() method to add Transient variable  RSS feed

 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
Haresh Kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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 ....
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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


 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I changed the the method to private and now getting the result thnks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!