I'm designing a game using Swing GUI and MVC method. And I'm trying to add the save and load capabilities to my game GUI. But the View cannot be serialized. So I used transient to define it's instance variable in my Controller. But when I load it the game does not load the view so I get Nullpointerexceptions as I call the instance variable of the game view in the GUI. Is there any solution for that ?
Asked
Active
Viewed 66 times
0
-
2You should not serialize the view but should serialize the state of the game. – randominstanceOfLivingThing Apr 24 '16 at 20:33
-
I do serialize the game state but according to the game state I use the control the view which leads me to using the view in the controller and again a nullpointer – Lose' CKi Apr 24 '16 at 20:52
-
For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 24 '16 at 23:26
1 Answers
0
You should not serialize the view but should serialize the state of the game. You have to make the view transient to avoid serialization.
When I do serialization usually I have two constructors:
1) One constructor without any parameters that I consider to use when say I create a new Game. I initialize my view in a separate method.
Game() {
initializeView();
}
2) Second constructor with state parameter that I consider to use when say I continue a Game. I initialize my view in a separate method.
Game(GameState state) {
initializeView();
//Restore game state here, I update GUI here
}

Community
- 1
- 1

randominstanceOfLivingThing
- 16,873
- 13
- 49
- 72
-
I did make it transient and I do serialize the state but I get the nullpointer – Lose' CKi Apr 24 '16 at 21:00
-
if I initialize the view it's going to be an empty view because I add every element in the view using the controller to add the action listener to it – Lose' CKi Apr 24 '16 at 21:10