Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

class invalid for deserialization

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help. Here is the code that I used to create foo.ser:

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 22458
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf, I recreated the savebox file and it worked properly. Thanks for your help.
 
and POOF! You're gone! But look, this tiny ad is still here:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic