Java - How do you avoid a StackOverflow after creating multiple objects?

fl_sh :

I am currently working on my exam-project for uni. The task is to create the boardgame ludo. After writing some code and doing some testing, I ran into a StackOverflowError.

So here is the structure (5 classes are essential): Main.class GUI.class Game.class Player.class Piece.class

Main creates a new object of type GUI called mainGUI. This creates the visuals for the game, including a small settings area with a start button.

On the press of the Start button, a new object of the type Game is created which then creates 4 new Objects of the type Player (4 players obviously).

When creating an object of type Player, this type gets the argument 'nmbr' which just states the Number of the Player (Player1, Player2 and so on).

Each Player has 4 pieces to move around on the board, so each of those 4 Players create another 4 objects of type Piece.

Now when pressing the Start button, what should happen is, that the pieces are displayed on the board. But that doesn't happen. Instead I get an errormessage stating, that there is a StackOverflowError on the call of the first Player object.

So I tried to read into the behaviour of Object creation in java and StackOverflow and stuff like that. But the only conclusion I can get right here, is that I've created too many objects inside one another.

public class Main {
    public static void main(String[] args){
        Main start = new Main();
        start.activate();
    }

    static GUI mainGui;

    public Main() {
       mainGui = new GUI();

    }

Inside the GUI there is the JButton 'submit' This button is supposed to start the game by creating the an object of the type Game.

submit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object btn = e.getSource();
                boolean ACTIVE = ((JButton) btn).isEnabled();
                if (ACTIVE) {
                    submit.setVisible(false);
                    cancel.setVisible(true);
                    Game spiel = new Game();
                    spiel.begin();
                }
            }
        });
public class Game {

    public Game() {

    }

    Player player1 = new Player(1);   //this is where the Error occurs
    Player player2 = new Player(2);
    Player player3 = new Player(3);
    Player player4 = new Player(4);
}
etc.
public class Player extends Game {

    private String name;
    private Color color;
    private boolean artiPlayer;
    private int playerNmbr, start, end;
    private int[] startArea, endArea;
    Piece piece1, piece2, piece3, piece4;

    public Player(int nmbr){
        if (nmbr == 1) {    
            System.out.println("1.1");
            piece1 = new Piece(500,175, Color.BLUE, Main.mainGui);
            piece2 = new Piece(550, 175, Color.BLUE, Main.mainGui);
            piece3 = new Piece(500,125, Color.BLUE, Main.mainGui);
            piece4 = new Piece(550, 125, Color.BLUE, Main.mainGui);
            start = 0;
            end = 64;
     }
}

Type Piece => Piece(xPos, yPos, Color, GUI)
//and so on

Here is the exact error message:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at spielPackage.Player.<init>(Player.java:18)
    at spielPackage.Game.<init>(Game.java:9)

Sorry if the code is a bit unclean. I am fairly new to java and this is still a work in progress.

At this point I am lost as to why Java throws a StackOverflowError

luk2302 :

Your Player should not extend Game since a Player is not a Game.

Right now you create a game which creates four players with every one of them being a different "Game" and creating four more players which are games and create four new players each......

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=311438&siteId=1