This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes updated object no longer loadable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "updated object no longer loadable" Watch "updated object no longer loadable" New topic
Author

updated object no longer loadable

Daniel Gallant
Greenhorn

Joined: Sep 09, 2009
Posts: 27
Hi!

I have a program that is currently being used to create characters for a game. Recently, in order to fix a bug, I had to modify the character object (I added two methods). Now I can't load the old saved characters. My solution was to rename the original object from CharacterRecord to ChararacterRecord100. I tried loading the original object using ChararacterRecord100, but I get an error:

icons_character_folio.CharacterRecord; local class incompatible: stream classdesc serialVersionUID = 124960255546936769, local class serialVersionUID = 124960255546936770

Can anyone make a suggestion on how I can get around this issue?

Thanks for your help!
Jimi Svedenholm
Ranch Hand

Joined: May 19, 2001
Posts: 53
Daniel Gallant wrote:Hi!

I have a program that is currently being used to create characters for a game. Recently, in order to fix a bug, I had to modify the character object (I added two methods). Now I can't load the old saved characters. My solution was to rename the original object from CharacterRecord to ChararacterRecord100. I tried loading the original object using ChararacterRecord100, but I get an error:

icons_character_folio.CharacterRecord; local class incompatible: stream classdesc serialVersionUID = 124960255546936769, local class serialVersionUID = 124960255546936770

Can anyone make a suggestion on how I can get around this issue?

Thanks for your help!


I assume that you saved the character objects using serialization? By default Java calculates a unique (well semi unique) version-identifier (serialVersionUID) for each class based on its fields etc.
Well, you can read about it here:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html

In short, in order to de-serialize a object, the version-identifier of the stream of the soon-to-be-object (ie the ones you saved before) must match the version-identifier of the class as it is today. But since you have modified the class this fails. The way around this is to specifically set the version-id to the same value as was calculated to the old version of the class (ie the first serialVersionUID in the error message).

So add this field to your class and it should probably solve this problem:

private static final long serialVersionUID = 124960255546936769L;

Hope this helps.

/Jimi
Daniel Gallant
Greenhorn

Joined: Sep 09, 2009
Posts: 27
I added "private static final long serialVersionUID = 124960255546936769L;" to the new class, but now I get a new error:

cannot assign instance of icons_character_folio.CharacterRecord to field icons_character_folio.Power.ch of type icons_character_folio.CharacterRecord101 in instance of icons_character_folio.Healing

I get that error when I do this:

och = (CharacterRecord)in.readObject();

Any help is appreciated.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You'll need to use the same class names as well as the same serialVersionUID.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Daniel Gallant
Greenhorn

Joined: Sep 09, 2009
Posts: 27
Ok, so how do I load the old file saved with the original CharacterRecord object to convert it to the new object?

Again, thanks for your help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

If the only changes are the methods (i.e. you have the same field names, types and order), then the two classes are actually compatible. After all, the data of the class is the same. Simply use the same serialVersionUID, same class name, and as far as the (de)serialization process is concerned it's the same class.
Daniel Gallant
Greenhorn

Joined: Sep 09, 2009
Posts: 27
GAH!!! I feel so dumb!

I just used the new class and it worked.

I want to thank you all for your help!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: updated object no longer loadable
 
Similar Threads
Deserialization error in JMS
Jasper Reports
Invalid Class Exception
Object deserialiaztion .... with different versionUid
Serialization