By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,886 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,886 IT Pros & Developers. It's quick & easy.

Load saved game from file using ObjectInputStream

P: 42
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:

Expand|Select|Wrap|Line Numbers
  1. private Game readObject(String n)throws IOException, ClassNotFoundException{
  2.         ObjectInputStream ois = null;
  3.         Game ga = null; 
  4.         //Construct the ObjectInputStream object
  5.         ois = new ObjectInputStream(new FileInputStream("res/"+n+".gla"));/
  6.  
  7.         Object obj = ois.readObject(); //this should somehow be made to read the entire file? 
  8.         if (obj instanceof Game) { //obviosly this is not enough. Returns always null.
  9.  
  10.             ga = (Game)obj;
  11.         }
  12.         if (ois != null) {
  13.             ois.close();
  14.         }
  15.         return ga;
  16.     }
Object's class variables:

Expand|Select|Wrap|Line Numbers
  1. private final static long serialVersionUID = 1;
  2. private Tavern tavern;
  3. private Blacksmith blacksmith;
  4. private Spellshop spellshop;
  5. private Vector<Team> teams;
  6. private Team activeTeam;
  7. private Gladiator currentGladiator = null;
  8. private Season season;
  9. private Battle battle;
  10. private int currentseason;
  11. private int humanplayers = 0;
Any help or insight upon the matter would be greatly appreciated.
Jun 26 '09 #1
Share this Question
Share on Google+
10 Replies


Expert 10K+
P: 11,448
@Humakt
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
Jun 26 '09 #2

P: 42
@JosAH
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:

Expand|Select|Wrap|Line Numbers
  1. private void writeObject(String file)throws IOException{
  2.         FileOutputStream fos = new FileOutputStream("res/"+file+".gla");
  3.         ObjectOutputStream oos = new ObjectOutputStream(fos);
  4.         oos.writeObject(this);
  5.         oos.close();
  6.     }
Jun 26 '09 #3

Expert 10K+
P: 11,448
@Humakt
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
Jun 26 '09 #4

10K+
P: 13,264
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.
Jun 26 '09 #5

P: 42
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:

Expand|Select|Wrap|Line Numbers
  1. public Game loadGame(String n){
  2.         Game ga = null;
  3.         try {
  4.             ga = this.readObject(n);
  5.         } catch (EOFException ex) { //This exception will be caught when EOF is reached
  6.         } catch (ClassNotFoundException ex) {
  7.             ex.printStackTrace();
  8.         } catch (FileNotFoundException ex) {
  9.             ex.printStackTrace();
  10.         } catch (IOException ex) {
  11.             ex.printStackTrace();
  12.         } 
  13.         if(ga==null)System.out.println("interesting...");
  14.         else System.out.println("Very interesting");
  15.         return ga;
  16.     }
New code for readObject:
Expand|Select|Wrap|Line Numbers
  1.  private Game readObject(String n)throws IOException, ClassNotFoundException{
  2.         ObjectInputStream ois = null;
  3.         Game ga = null; 
  4.         //Construct the ObjectInputStream object
  5.         ois = new ObjectInputStream(new FileInputStream("res/"+n+".gla"));
  6.         Object obj = null;
  7.         boolean found = false;
  8.         while((obj = ois.readObject())!= null&&!found){
  9.             if (obj instanceof Game) {
  10.  
  11.                 ga = (Game)obj;
  12.                 System.out.println(ga.getActiveTeamName()); //these two lines print what was excepted
  13.                 System.out.println(ga.getCurrentGladiator().getName());
  14.                 found = true;
  15.             }
  16.         }
  17.  
  18.         System.out.println(ga.getActiveTeamName()); //doesn't get here
  19.         if (ois != null) {
  20.             ois.close();
  21.         }
  22.         return ga;
  23.     }
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..
Jun 26 '09 #6

Expert 10K+
P: 11,448
@Humakt
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
Jun 27 '09 #7

P: 42
Sorry, was on vacation. I am back now and problem still seems to persist.

@JosAH
Yes, I posted the function for it above. I am using Eclipse as an IDE.
Jul 14 '09 #8

Expert 10K+
P: 11,448
@Humakt
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
Jul 15 '09 #9

P: 42
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.
Jul 15 '09 #10

Expert 10K+
P: 11,448
@Humakt
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
Jul 15 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.