Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialisation problem

 
GregK
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you use an ObjectOutputStream to write objects of the same class in the same order? Sounds like you wrote Strings to the file.
 
GregK
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I wrote 2 strings to a file, can this not be done and deserialised just the same?
 
GregK
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic