File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Serialisation problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serialisation problem" Watch "Serialisation problem" New topic
Author

Serialisation problem

GregK
Greenhorn

Joined: Oct 24, 2005
Posts: 15
Hi

Basically I wantto do a save and a load of 2 variables. Which seems to work because the save file stores them when I look to see.

It's loading the file to the fields where I get problems - java.lang.ClassCastException: java.lang.String



theres the load game code.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Did you use an ObjectOutputStream to write objects of the same class in the same order? Sounds like you wrote Strings to the file.


[How To Ask Questions On JavaRanch]
GregK
Greenhorn

Joined: Oct 24, 2005
Posts: 15
Yes I wrote 2 strings to a file, can this not be done and deserialised just the same?
GregK
Greenhorn

Joined: Oct 24, 2005
Posts: 15
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

No. How is ObjectInputStream supposed to know that a String in a file corresponds to a Room object? To read a Room out, you have to serialize a Room instance.
GregK
Greenhorn

Joined: Oct 24, 2005
Posts: 15
sorry i'm new to serilization, which is why i posted in the beginner forum.

How do I do something like that?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Don't sweat it. I'm being short because I'm at work
You write just like you read:

The classes Room and Player must implement the java.io.Serializable interface (there's no methods, just declare them) so the output stream knows they can be written out.
The Java Tutorial has a chapter on Input and Output if you want to know more.
[ March 24, 2006: Message edited by: Joe Ess ]
GregK
Greenhorn

Joined: Oct 24, 2005
Posts: 15
thanks for that. :-)

Now I get the - java.io.NotSerializableException: Room
What does this mean?>

I've already imported Java.io.Serializable and Java.io.*

[ March 24, 2006: Message edited by: Gregory Kodikara ]
[ March 24, 2006: Message edited by: Gregory Kodikara ]
GregK
Greenhorn

Joined: Oct 24, 2005
Posts: 15
Oh god im an idiot
I just realised I hadnt added the 'implements serializable' line to the class names in all classes (I got the same exception for each class)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialisation problem