Load saved game from file using ObjectInputStream 
June 26th, 2009, 12:11 PM
| | Member | | Join Date: Aug 2008
Posts: 36
| |
I'm trying to figure a good way to handle saving game state to file (common save/load game function in games). ObjectOutput/InputStream seems most promising so far.
However,the object I'm saving contains lots of other objects which in turn contain other objects. All objects implement Serializable (I've given them all serialVersionUID = 1, not sure whether it matters).
It seems to write the object properly but loading it is a bit of a problem.
This is the function I'm using: - private Game readObject(String n)throws IOException, ClassNotFoundException{
-
ObjectInputStream ois = null;
-
Game ga = null;
-
//Construct the ObjectInputStream object
-
ois = new ObjectInputStream(new FileInputStream("res/"+n+".gla"));/
-
-
Object obj = ois.readObject(); //this should somehow be made to read the entire file?
-
if (obj instanceof Game) { //obviosly this is not enough. Returns always null.
-
-
ga = (Game)obj;
-
}
-
if (ois != null) {
-
ois.close();
-
}
-
return ga;
-
}
Object's class variables: - private final static long serialVersionUID = 1;
-
private Tavern tavern;
-
private Blacksmith blacksmith;
-
private Spellshop spellshop;
-
private Vector<Team> teams;
-
private Team activeTeam;
-
private Gladiator currentGladiator = null;
-
private Season season;
-
private Battle battle;
-
private int currentseason;
-
private int humanplayers = 0;
Any help or insight upon the matter would be greatly appreciated.
| 
June 26th, 2009, 12:26 PM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: Load saved game from file using ObjectInputStream Quote:
Originally Posted by Humakt It seems to write the object properly but loading it is a bit of a problem. | Care to elaborate a bit on the 'bit of a problem'? Doesn't it read the object from the Stream? Make sure that all your member objects of a Game object are Serializable or make them transient.
btw, if you're using an IDE most likely it can generate a VersionID value for you.
kind regards,
Jos
| 
June 26th, 2009, 01:13 PM
| | Member | | Join Date: Aug 2008
Posts: 36
| | | re: Load saved game from file using ObjectInputStream Quote:
Originally Posted by JosAH Care to elaborate a bit on the 'bit of a problem'? Doesn't it read the object from the Stream? Make sure that all your member objects of a Game object are Serializable or make them transient.
btw, if you're using an IDE most likely it can generate a VersionID value for you.
kind regards,
Jos | Well, I think the problem is that there are multiple objects in the file, but they are all (one way or other) part of this game object. I tried reading object in loop as well but it also returned null.
I correct the first post in sec.
This is the function I use to write the object: - private void writeObject(String file)throws IOException{
-
FileOutputStream fos = new FileOutputStream("res/"+file+".gla");
-
ObjectOutputStream oos = new ObjectOutputStream(fos);
-
oos.writeObject(this);
-
oos.close();
-
}
| 
June 26th, 2009, 01:54 PM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: Load saved game from file using ObjectInputStream Quote:
Originally Posted by Humakt Well, I think the problem is that there are multiple objects in the file, but they are all (one way or other) part of this game object. I tried reading object in loop as well but it also returned null. | For debugging make all your member objects transient and try again; you should be able to write/read an (almost) empty Game object. Next add the member objects again and give them another SerialVersionUID number and try again. Your code looks reasonable.
kind regards,
Jos
| 
June 26th, 2009, 01:55 PM
| | Administrator | | Join Date: Sep 2006
Posts: 12,084
| | | re: Load saved game from file using ObjectInputStream
You are telling us what you think is causing the problem not the actual results of your program.
Basically you must read what you have written. If you write a Jos, read back a Jos, not a JosAH, Loretta or any other such variants thereof.
| 
June 26th, 2009, 04:38 PM
| | Member | | Join Date: Aug 2008
Posts: 36
| | | re: Load saved game from file using ObjectInputStream
Sorry, was in a hurry earlier. Seems that ObjectInputStream does find the object in question (I tried printing values from object's objects) inside the loop.
However it seems it gets stuck in exception and the function doesn't have the chance to return the object.
These are the exceptions I'm testing in function: - public Game loadGame(String n){
-
Game ga = null;
-
try {
-
ga = this.readObject(n);
-
} catch (EOFException ex) { //This exception will be caught when EOF is reached
-
} catch (ClassNotFoundException ex) {
-
ex.printStackTrace();
-
} catch (FileNotFoundException ex) {
-
ex.printStackTrace();
-
} catch (IOException ex) {
-
ex.printStackTrace();
-
}
-
if(ga==null)System.out.println("interesting...");
-
else System.out.println("Very interesting");
-
return ga;
-
}
New code for readObject: -
private Game readObject(String n)throws IOException, ClassNotFoundException{
-
ObjectInputStream ois = null;
-
Game ga = null;
-
//Construct the ObjectInputStream object
-
ois = new ObjectInputStream(new FileInputStream("res/"+n+".gla"));
-
Object obj = null;
-
boolean found = false;
-
while((obj = ois.readObject())!= null&&!found){
-
if (obj instanceof Game) {
-
-
ga = (Game)obj;
-
System.out.println(ga.getActiveTeamName()); //these two lines print what was excepted
-
System.out.println(ga.getCurrentGladiator().getName());
-
found = true;
-
}
-
}
-
-
System.out.println(ga.getActiveTeamName()); //doesn't get here
-
if (ois != null) {
-
ois.close();
-
}
-
return ga;
-
}
I am using Swing and MVC model and also DAO (but none of them are part of the objects Game object knows).
EDIT: Ok. End of file exception seems to be the reason. Trying to fix it..
| 
June 27th, 2009, 12:54 PM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: Load saved game from file using ObjectInputStream Quote:
Originally Posted by Humakt EDIT: Ok. End of file exception seems to be the reason. Trying to fix it.. | Are you sure you're closing the ObjectOutputStream after writing and not the wrapped FileOutputStream? There might still be something in the buffer of the first stream that doesn't get written that way.
kind regards,
Jos
| 
July 15th, 2009, 12:47 AM
| | Member | | Join Date: Aug 2008
Posts: 36
| | | re: Load saved game from file using ObjectInputStream
Sorry, was on vacation. I am back now and problem still seems to persist. Quote:
Originally Posted by JosAH Are you sure you're closing the ObjectOutputStream after writing and not the wrapped FileOutputStream? There might still be something in the buffer of the first stream that doesn't get written that way.
kind regards,
Jos | Yes, I posted the function for it above. I am using Eclipse as an IDE.
| 
July 15th, 2009, 09:36 AM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: Load saved game from file using ObjectInputStream Quote:
Originally Posted by Humakt Sorry, was on vacation. I am back now and problem still seems to persist.
Yes, I posted the function for it above. I am using Eclipse as an IDE. | Make Eclipse generate those version IDs for the classes that are saved because they're part of your Game object (it's a 'quick fix' in Eclipse). You're logically writing one object so you read one object (there's no loop involved). Show us the Exception stack trace that is thrown.
kind regards,
Jos
| 
July 15th, 2009, 04:07 PM
| | Member | | Join Date: Aug 2008
Posts: 36
| | | re: Load saved game from file using ObjectInputStream
Seems that it is working now that I made Eclipse generate them (also made small adjustments). I'm bit curious though, will I always have to make Eclipse regenerate version ID's when I make adjustments to class implementing serializable?
Big thanks for the help.
| 
July 15th, 2009, 05:35 PM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: Load saved game from file using ObjectInputStream Quote:
Originally Posted by Humakt Seems that it is working now that I made Eclipse generate them (also made small adjustments). I'm bit curious though, will I always have to make Eclipse regenerate version ID's when I make adjustments to class implementing serializable?
Big thanks for the help. | You're welcome of course; w.r.t. that ID: better do so because two different classes with a same ID number make no sense: i.e. they are loaded but the fields may not correspond. When everything is stable you don't need to change that ID number anymore. Also read the API documentation for the Serializable interface; it explains everything for that ID number.
kind regards,
Jos
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|