Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class invalid for deserialization

 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code. Running in NetBeans. I'm receiving an error that "class invalid for deserialization"

What do I need to do here to prepare the class for deserialization?

Thank you.


 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bud,

Hmmm. That error message is generally only going to come up in weird situations in which a class implements the Serializable interface, objects of the class are serialized, the class is then modified to not implement Serializable, and then you try to deserialize the contents of the file. Given that you're trying to extract Integers from the file, though, that explanation doesn't make sense, since it only applies if you're deserializing a user-supplied class.

So... how did you create the file foo.ser? What's really in it?
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. Here is the code that I used to create foo.ser:

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:That error message is generally only going to come up in weird situations in which a class implements the Serializable interface, objects of the class are serialized, the class is then modified to not implement Serializable, and then you try to deserialize the contents of the file.

Bingo!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bud,

OK, that explains it, then! You saved a SaveBox in the file, and so Java is going to look for a class file named SaveBox to load when the file is read. But the SaveBox it finds when the class is read is not serializable, so this isn't going to work! You need to do two things differently:

1) To load foo.ser, you need SaveBox.class to exist, and (most importantly!) it needs to be the same class that was saved into the file -- i.e. the second one you showed me. Therefore, you're going to have to name your file reading class something else!

2) Once you fix that problem, your program will still fail. That's because you wrote a SaveBox, but you're trying to read two Integers. You need to read a SaveBox instead. Whatever you write ot the file, that's what you're going to get back out. So...


Your SaveBox class doesn't have getWidth and getHeight -- you'll need to add those.
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, Thanks for your help. I think I understood what you said. Here are the two updated files based on your comments. I'm still getting an error on lines 25 and 26 saying that it can't find the symbol svbox. The program is giving errors when I run it in NetBeans.

To write the file:



To read the file:


 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, svbox is known in main, but not in go. You could declare the go method to take a SaveBox parameter, and thus pass svbox to it.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or drop svbox completely and use the local variable box instead.
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. O.K., I've declared the go method to take a SaveBox parameter. below is the code. Now I'm getting an error at runtime: "java.io.InvalidClassException: savebox.SaveBox; local class incompatible: stream classdesc serialVersionUID = -9024886691042451633, local class serialVersionUID = -1451760072563593720"

Also it's printing out that width and height are both = 0. They were set as 20 and 50 when I saved SaveBox.

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.InvalidClassException: savebox.SaveBox; local class incompatible: stream classdesc serialVersionUID = -9024886691042451633, local class serialVersionUID = -1451760072563593720

Did you make any changes to the SaveBox class after you created the .ser file? Try creating and reading a new .ser file.

Also it's printing out that width and height are both = 0. They were set as 20 and 50 when I saved SaveBox.

That's just a consequence of deserialization being unsuccessful.
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf, I recreated the savebox file and it worked properly. Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic