Google Gson Causing A Stackoverflow
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
private
transient
Player player;
field, to avoidStackOverflowError
- 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"