Zach443
Zach443

Reputation: 311

Massive StackOverflowError from GSON toJson

So I am trying to use GSON to convert a class into a JSON file with the toJson() function and every time the code gets called there is a massive 1000+ line StackOverflowError. This is the error: http://pastebin.com/dBhYUFva

And here is the class I am doing gson.toJson() on:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import mc.kwit.arena.players.ArenaPlayer;
import mc.kwit.arena.util.PlayerUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zach on 8/3/16.
 */
public class Match {

    List<ArenaPlayer> arenaPlayers = new ArrayList<ArenaPlayer>();
    String[] uuidArray = null;
    Player winner;
    String matchId = "123456";
    int killLimit = 5;
    int timeLimit = -1;


    public Match() {

    }

    public Match(List<ArenaPlayer> arenaPlayerList, String id, int seconds, int kills) {
        this.arenaPlayers = arenaPlayerList;
        this.matchId = id;
        this.timeLimit = seconds;
        this.killLimit = kills;
    }

    public Match(String[] players, String id, int seconds, int kills) {
        this.uuidArray = players;
        this.matchId = id;
        this.timeLimit = seconds;
        this.killLimit = kills;
    }

    public void start() {

        //Create our ArenaPlayer objects
        this.arenaPlayers = PlayerUtil.uuidToArenaPlayerList(uuidArray);

        Bukkit.getLogger().info("Match \" " + this.matchId + "\" has been started!");
        //Whitelist all participants
        for(ArenaPlayer p : arenaPlayers) {
            KwitArena.pc.addArenaPlayer(p.getPlayer());
            p.getPlayer().setWhitelisted(true);
            Bukkit.getLogger().info(p.getName() + " has been whitelisted!");
            p.setMatch(this);
        }
    }

    public void finish() throws Exception {

        //Remove all players from whitelist and game
        for(ArenaPlayer p : arenaPlayers) {
            p.getPlayer().setWhitelisted(false);

            if(p.isWinner()){
                p.kick(ChatColor.GREEN + "You have won the game!");
                this.winner = p.getPlayer();
            } else {
                p.kick(ChatColor.RED + winner.getName() + "has won the game :(");
            }
        }

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String stats = gson.toJson(this);
        KwitArena.postStats(stats);

        //Remove this arena instance from the plugin
        KwitArena.match = null;
    }

    public String getId(){
        return(this.matchId);
    }

    public void setWinner(ArenaPlayer p) {
        this.winner = p.getPlayer();
    }
}

ArenaPlayer.class :

import mc.kwit.arena.Match;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

/**
 * Created by Zach on 7/31/16.
 */
public class ArenaPlayer {

    ArenaPlayer ap;
    Player  player;
    OfflinePlayer offlinePlayer;
    int kills = 0;
    int deaths = 0;
    boolean isInGame = false;
    Match match = null;
    boolean isWinner = false;

    public ArenaPlayer(Player player){
        this.player = player;
    }

    public ArenaPlayer(Player player, boolean playing, int numKills, int numDeaths) {
        this.player = player;
        kills = numKills;
        deaths = numDeaths;
        isInGame = playing;
    }


    //Getters
    public String getMatchId() { return(this.match.getId());}

    public int getKills() { return(this.kills);}

    public int getDeaths() { return(this.deaths);}

    public Match getMatch() { return(this.match);}

    public Player getPlayer() { return(this.player);}

    public String getName() { return(player.getName());}

    public OfflinePlayer getOfflinePlayer() { return(this.offlinePlayer);}

    //Setters
    public void setMatch(Match match) { this.match = match;}

    public void setIsWinner(boolean b) { this.isWinner = b;}

    public void setOfflinePlayer(OfflinePlayer off) { this.offlinePlayer = off; }


    //Extras
    public void addDeath() { this.deaths++;}

    public void addKill() { this.kills++;}

    public boolean isPlaying() { return(this.isInGame);}

    public boolean isWinner() { return(this.isWinner);}

    public void kick(String message) { player.kickPlayer(message);}


}

I'm not really sure where to even begin in the stack trace, anyone have some pointers?

Upvotes: 1

Views: 1383

Answers (1)

Bohemian
Bohemian

Reputation: 425318

I think the problem is that Match has an ArenaPlayer field, and ArenaPlayer has a Match field, which if the two instances reference each other causes Gson to go into an infinite loop due to its implementation.

You'll have to detangle your classes, your instances, or use a different library, or exclude certain fields from serialization - see Gson doc.


BTW, 1000 is the default call stack depth - if you see this many lines in the stack, you've probably hit an infinite recursion.

Upvotes: 2

Related Questions