0

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 ?

randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
Lose' CKi
  • 31
  • 6

1 Answers1

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