Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Tolls

Thread: save/load - serialization/deserialization

  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default save/load - serialization/deserialization

    Hi, I have a chess game that I am trying to save to a .ser file and then load.

    My main class is serialized like this:
    Java Code:
    public class Board extends JFrame implements Serializable
    My save class has a method that looks like this:
    Java Code:
    public void saveGameAction()
        {
            try
            {
                FileOutputStream fs = new FileOutputStream("test.ser");
                ObjectOutputStream os = new ObjectOutputStream(fs);
                os.writeObject(b);
                os.close();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    Here variable b contains the Board class which we want to restore.

    My load class has this method:
    Java Code:
    public void loadGameAction()
        {
            try
            {
                FileInputStream fileStream = new FileInputStream("test.ser");
                ObjectInputStream is = new ObjectInputStream(fileStream);
                Board restore = (Board) is.readObject();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    Trying to load test.ser which I think I save correctly in the save class, now do I need a method in Board which I call like this to make this work?
    Java Code:
    restore.restoreMethod();
    If I do like this, does the variables after loading contain the saved values that I in this method just use to place my chess pieces on its old positions again? I ask this because the current code does nothing, all chess pieces are at their starting positions if I load a file that had its pieces moved and a variable just containing an int is also not restored to its supposedly saved value.
    Last edited by Gatsu; 02-19-2013 at 02:20 PM.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: save/load - serialization/deserialization

    Hi Gatsu,

    The code looks ok but it doesn't look as though the board being painted is the same one being created in the loadGameAction() method.

    I'm a little confused by the mention of the three classes, main, save and load. At the least I would expect there to be a single class which constructs an instance of board. This same class would contain the save and load methods and reference the single board instance.

    Regards.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: save/load - serialization/deserialization

    There can be a lot of issues regarding serialization. For one, are your chess pieces stored in static variables or variables marked transient. If they are, then they will not be serialized.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: save/load - serialization/deserialization

    Quote Originally Posted by Ronin View Post
    Hi Gatsu,

    The code looks ok but it doesn't look as though the board being painted is the same one being created in the loadGameAction() method.

    I'm a little confused by the mention of the three classes, main, save and load. At the least I would expect there to be a single class which constructs an instance of board. This same class would contain the save and load methods and reference the single board instance.

    Regards.
    In the Board class I also have the main method that does:
    Java Code:
    Board b = new Board();
    And then delcare this in the start of the class:
    Java Code:
    Board b;
    So this b is not the same as the one I create in the main method in the same class?

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: save/load - serialization/deserialization

    Java Code:
    Board restore = (Board) is.readObject();
    You read the saved Board and store the reference in 'restore'.
    Then do nothing with that reference, which goes out of scope and so the object is probably quickly reclaimed during garbage collection.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: save/load - serialization/deserialization

    Quote Originally Posted by Tolls View Post
    Java Code:
    Board restore = (Board) is.readObject();
    You read the saved Board and store the reference in 'restore'.
    Then do nothing with that reference, which goes out of scope and so the object is probably quickly reclaimed during garbage collection.
    I tried doing anything with 'restore'.
    I add this in Board class:
    Java Code:
    public void aa()
        {
            System.out.println("aa");
        }
    then try restore.aa(); in the load method but it gives me this error at that line:
    java.lang.NullPointerException



    Java Code:
    FileInputStream fis = new FileInputStream("test.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Board restore = (Board) ois.readObject();
    restore.aa();
    ois.close();

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: save/load - serialization/deserialization

    What's the full stack trace?
    Is it the restore.aa() call giving the exception?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: save/load - serialization/deserialization

    I'm sorry, a full stack trace would look like this and yes that is the line causing it:

    java.lang.NullPointerException
    at chess.LoadGame.loadGameAction(LoadGame.java:34)
    at Chess.Board$3.actionPerformed(Board.java:137)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton. java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Bas icMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 693)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    I have a load button in my menu that runs LoadGame.loadGameAction() which is the load method containing restore.aa().
    Last edited by Gatsu; 02-20-2013 at 02:49 PM.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: save/load - serialization/deserialization

    So you've saved a null object.
    That's the only thing that can have happened, based on the code in #6.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: save/load - serialization/deserialization

    Ok thanks, I moved the save method to the Board class to make it save correctly, and now I can do restore.aa();

    Now I have to create a method that will do something to restore all variables? I cant find any tutorial that does anything else then this step to restore an object:

    Board restore = (Board) ois.readObject();

    I get no error message nor anything is hapening when I load.
    Last edited by Gatsu; 02-21-2013 at 02:09 AM.

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: save/load - serialization/deserialization

    So now you've restored your Board.
    But you haven't displayed it anywhere (I think the visible flag is transient), so I would pack() and setVisible(true).

    Remember, you have saved the entire Window, here, so this will open a new one (if it works).
    Gatsu likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: save/load - serialization/deserialization

    thank yo uvery much, aparently actionListeners also wont get serialized but I solved it by making a new method for adding all those and calling that method when loading, it made my code more clean also, thanks it's working great!

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: save/load - serialization/deserialization

    This is one of the reasons most saves are done using just a data model, rather than the model and the GUI.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 03-22-2012, 04:26 PM
  2. setTooltipText broken after serialization/deserialization
    By tylerdurden in forum AWT / Swing
    Replies: 7
    Last Post: 03-04-2011, 12:45 AM
  3. Singleton serialization / deserialization
    By DerekRaimann in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 01:38 AM
  4. Serialization/Deserialization Error
    By andrepezzo in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2008, 05:36 PM
  5. Serialization/Deserialization Error
    By andrepezzo in forum Networking
    Replies: 0
    Last Post: 12-16-2008, 04:21 PM

Posting Permissions

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