Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException upon deserialization of transient variable.  RSS feed

 
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
So I'm trying to figure out serialization. Below is the code and it's saving object state.



now upon trying to de-serialize I'm getting NullPointer on transient variable.
I know that transient variable get's a null value but it's not getting changed even if I explicitly try to do so.



Edit 1 : Now I figured there's something wrong with de-serialization code apparently everything is getting null.
Edit 2 : I'm getting Local class incompatible with readObject() method. How to solve the problem ?
Edit 3 : Lol I'm stupid. I changed a variable to my initial serialization code. But was trying to de-serialize the new variable without saving that obj in file.
Now it seems I can't delete the thread. so marking as resolved.
 
Bartender
Posts: 1460
31
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good work, sticking with your problem until you worked it out yourself, Shubham.

Have a cow for your tenacity!
 
Marshal
Posts: 59095
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already know that transient means that the field is not included when you serialise an object. Static fields are also similarly omitted (I think). When you deserialise the object, it is as if you called a public no‑arguments constructor, as you have in those classes. I shall leave you to find the constructor. Then you re‑initialise the fields to the values in the .ser file. Obviously the transient field will not be reassigned.

The transient keyword is intended for things like passwords which you do not want to record in a .ser file.
 
Campbell Ritchie
Marshal
Posts: 59095
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are calling the getStats method before the object is de‑serialised, you are trying to use a null.
 
Sheriff
Posts: 21328
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Static fields are also similarly omitted (I think).


They indeed are.

The transient keyword is intended for things like passwords which you do not want to record in a .ser file.


I'd say it's more intended for things that simply can't be serialized like sockets, file handles or even iterators.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The transient keyword is intended for things like passwords which you do not want to record in a .ser file.



Do we have to save in .ser file as per convention ? As you can see I have saved in .txt and it works fine.
 
Sheriff
Posts: 23506
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you suspect, you can name the file whatever you like.
 
Campbell Ritchie
Marshal
Posts: 59095
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the convention would be to use .ser or .obj as extensions.
 
Campbell Ritchie
Marshal
Posts: 59095
180
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:As you suspect, you can name the file whatever you like.

Depends whether your text editor can open it. You will get no end of confusion if you find a .txt file next year and cannot read its contents.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: You will get no end of confusion if you find a .txt file next year and cannot read its contents.


lol. I can totally relate. It'll definitely end up in my recycle bin.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!