Skip to content Skip to sidebar Skip to footer

Google Gson Causing A Stackoverflow

Error 07-14 04:33:28.030: E/AndroidRuntime(5216): FATAL EXCEPTION: main 07-14 04:33:28.030: E/AndroidRuntime(5216): Process: com.tt, PID: 5216 07-14 04:33:28.030: E/An

Solution 1:

Player has Game, Game has Player, Player has Game, Game has Player... stackoverflow.

Change the structure of your classes. Eliminate circular references, or use transient keyword on Player or Game reference to exclude it from the serialization.

EDIT:

If you don't want to change the structure of your classes to for example: each Game has reference to the Player, but the Player doesn't have references to his Games. Then you may do something like this:

  • serialize the Player
  • Player will serialize its Games collection
  • the Game class should have privatetransientPlayer player; field, to avoid StackOverflowError
  • After deserialization the Player should iterate over its Games and call game.setPlayer(this); on each of them

Also you need fix your toString() methods. Because Player class's toString() method calls Game class's toString() which calls Player class's toString().

When you get StackOverflowError just look for things like this. Or don't build class structures like this, where each Player has his Games, and each Game has its Player.

Post a Comment for "Google Gson Causing A Stackoverflow"