Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By rob1794

Thread: Saving game objects to some relevant type of file that can be recalled (java)

  1. #1
    rob1794 is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Saving game objects to some relevant type of file that can be recalled (java)

    I am simulating a game. This involves creating two Player objects(either HumanPlayer or ComputerPlayer), a TheBoard object, a TheGame object and a TheGameManager object. In the TheGameManager, i am trying to get it so that i can save the two Player objects, the TheBoard object and the TheGame object in current use at some point in the game and then load them later at some point. However, after debugged, i have found that it doesnt like the writeObject() method and skips and sends an exception up. Please help, thanks.



    My TheGameManager class has the following fields:

    public class TheGameManager implements GameManager {

    private Player player1;
    private Player player2;
    private TheBoard currentBoard;
    private TheGame currentGame;
    //private String[] args;
    private int iterations = 1;

    private InputStreamReader isr;
    private BufferedReader input;
    private PrintStream output;
    private InputStream in;

    private boolean hasLoaded;
    private String[] args;



    My saveGame method is below:

    `public void saveGame(String fname) throws FileFailedException {

    try{
    String fileName = String.format("(%s).gam",fname);
    FileOutputStream fos = new FileOutputStream(fileName);
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(currentGame);
    oos.writeObject(currentBoard);
    oos.writeObject(player1);
    oos.writeObject(player2);
    oos.close();
    fos.close();
    System.out.println("Data has been saved as the file " + fileName +".");
    }
    catch(IOException e){
    throw new FileFailedException("File failed to save!");
    }
    }`




    My Load game method is below:

    public void loadGame(String fname) throws FileFailedException{
    try{
    String fileName = String.format("(%s).gam",fname);
    FileInputStream fis = new FileInputStream(fileName);
    ObjectInputStream ois = new ObjectInputStream(fis);

    try{
    currentGame = (TheGame) ois.readObject();
    }
    catch(ClassNotFoundException e){
    System.out.println("Could not find the 'TheGame' object in the specified file");
    }

    try{
    currentBoard = (TheBoard) ois.readObject();
    }
    catch(ClassNotFoundException e){
    System.out.println("Could not find the 'TheBoard' object in the specified file");
    }

    try{
    player1 = (Player) ois.readObject();
    }
    catch(ClassNotFoundException e){
    System.out.println("Could not find the 'Player' object in the specified file");
    }

    try{
    player2 = (Player) ois.readObject();
    }
    catch(ClassNotFoundException e){
    System.out.println("Could not find the 'Player' object in the specified file");
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Saving game objects to some relevant type of file that can be recalled (java)

    Help yourself and rather than running away from the exception, actually read it and try to understand what it is trying to tell you. The exception is your friend who is trying to help you by for example pointing out exactly which line of code is breaking, and why. Although the why is sometimes a bit vague.

    If you still can't figure it out then you'll need to not make a secret out of that exception and actually post it and also point out which line of code it is referring to.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Saving game objects to some relevant type of file that can be recalled (java)

    Java Code:
    } catch(IOException e){
        throw new FileFailedException("File failed to save!");
    }
    With this code (and the similar ones in the read code) you are losing all the information about the exception.
    You can either printStackTrace on the received exception or, as you are throwing a new one, pass the caught exception into the new one (essentially wrapping it):
    Java Code:
    new FileFailedException("blah blah blah", e);
    That will mean adding a new constructor that accepts and Exception object, and then passing that exception through to the parent.

    That way at least you can figure out where exactly things are going wrong.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Saving game objects to some relevant type of file that can be recalled (java)

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    rob1794 is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: Saving game objects to some relevant type of file that can be recalled (java)

    Thank you for your comments.

    Please do not post on this anymore as it is now an irrelevant question, thanks in advance!

  6. #6
    rob1794 is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: Saving game objects to some relevant type of file that can be recalled (java)

    Thank you for your replies, they are much appreciated!

    However, this question is now irrelevant. So for anyone reading this, please do not post anything else on this thread, thank you in advance.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Saving game objects to some relevant type of file that can be recalled (java)

    Quote Originally Posted by rob1794 View Post
    Thank you for your comments.

    Please do not post on this anymore as it is now an irrelevant question, thanks in advance!
    Quote Originally Posted by rob1794 View Post
    Thank you for your replies, they are much appreciated!

    However, this question is now irrelevant. So for anyone reading this, please do not post anything else on this thread, thank you in advance.
    This is a forum, where we share problems and their possible solutions. If your problem is solved, you should share the solution here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    rob1794 is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: Saving game objects to some relevant type of file that can be recalled (java)

    I have found out that i was trying to save a buffered reader as one of the fields which was causing problems so i used transient variables.
    DarrylBurke likes this.

Similar Threads

  1. issues while saving a '.java' file in the bin.
    By akshayshukla in forum New To Java
    Replies: 3
    Last Post: 04-29-2012, 03:02 PM
  2. help with saving objects in ArrayList
    By anghel.michael in forum New To Java
    Replies: 4
    Last Post: 02-22-2011, 09:09 PM
  3. Saving objects problem
    By BennyJass in forum Networking
    Replies: 2
    Last Post: 01-15-2011, 10:49 PM
  4. Saving Objects Permanently?
    By ethann_91 in forum New To Java
    Replies: 1
    Last Post: 01-20-2010, 08:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •