Results 1 to 13 of 13
- 02-19-2013, 01:41 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
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:
My save class has a method that looks like this:Java Code:public class Board extends JFrame implements Serializable
Here variable b contains the Board class which we want to restore.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(); } }
My load class has this method:
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: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(); } }
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.Java Code:restore.restoreMethod();
Last edited by Gatsu; 02-19-2013 at 02:20 PM.
- 02-19-2013, 02:29 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 02-19-2013, 02:42 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 688
- Rep Power
- 1
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.
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 09:08 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
Re: save/load - serialization/deserialization
- 02-20-2013, 10:00 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: save/load - serialization/deserialization
You read the saved Board and store the reference in 'restore'.Java Code:Board restore = (Board) is.readObject();
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.
- 02-20-2013, 12:03 PM #6
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
Re: save/load - serialization/deserialization
I tried doing anything with 'restore'.
I add this in Board class:
then try restore.aa(); in the load method but it gives me this error at that line:Java Code:public void aa() { System.out.println("aa"); }
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();
- 02-20-2013, 02:12 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 02-20-2013, 02:20 PM #8
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
Re: save/load - serialization/deserialization
I'm sorry, a full stack trace would look like this and yes that is the line causing it:
I have a load button in my menu that runs LoadGame.loadGameAction() which is the load method containing restore.aa().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)Last edited by Gatsu; 02-20-2013 at 02:49 PM.
- 02-20-2013, 03:16 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 02-20-2013, 08:58 PM #10
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
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.
- 02-21-2013, 09:32 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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).Please do not ask for code as refusal often offends.
- 02-21-2013, 03:40 PM #12
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
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!
- 02-21-2013, 04:00 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Problem with Object serialization/deserialization to GUI
By Allexxy in forum AWT / SwingReplies: 4Last Post: 03-22-2012, 04:26 PM -
setTooltipText broken after serialization/deserialization
By tylerdurden in forum AWT / SwingReplies: 7Last Post: 03-04-2011, 12:45 AM -
Singleton serialization / deserialization
By DerekRaimann in forum New To JavaReplies: 4Last Post: 02-28-2011, 01:38 AM -
Serialization/Deserialization Error
By andrepezzo in forum Advanced JavaReplies: 2Last Post: 12-16-2008, 05:36 PM -
Serialization/Deserialization Error
By andrepezzo in forum NetworkingReplies: 0Last Post: 12-16-2008, 04:21 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks