简体   繁体   中英

How do I solve this Stack Over Flow Error?

I am struggling to solve this StackOverFlow exception when I try to run my TicTacToe program.

From reading the stack trace I know that the StackOverFlow error is the result of a cyclic relationship. This is because when I instantiate the miniFrame class inside the TicTacToeFrame constructor, A TicTacToeFrame object is used as the parameter for the miniFrame constructor. As a result, this causes infinite recursion.

Exception in thread "main" Exception in thread "main" Exception in thread "main" java.lang.StackOverflowError
    at java.awt.Window.init(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Dialog.<init>(Unknown Source)
    at java.awt.Dialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at miniFrame.<init>(miniFrame.java:12)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)

TicTacToeFrame constructor instantiates the miniFrame dialogBox = new miniFrame (this) .

//IMPORT JAVA LIBARARY
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;

public class TicTacToeFrame extends JFrame {
   public char whoseTurn = 'X';   

   public Cell [] [] cells = new Cell [3] [3];
   JLabel jlblStatus = new JLabel ("X's turn to play");

   public boolean gameOver = false;

   public JPanel panel;

   JTextField textField;

public TicTacToeFrame () {
    miniFrame dialogBox = new miniFrame (this);//This is line 26 referred to in the stack trace
    dialogBox.returnName ();
    panel = new JPanel (new GridLayout (3, 3, 0, 0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            panel.add (cells [i] [j] = new Cell(this));
        }
    }

    panel.setBorder (new LineBorder (Color.red, 1));
    jlblStatus.setBorder (new LineBorder (Color.yellow, 1));

    add (panel, BorderLayout.CENTER);
    add (jlblStatus, BorderLayout.SOUTH);
}

This is the miniFrame constructor which takes an object of TicTacToeFrame as it's an argument. But I don't understand why the JVM finds a StackOverFlow error on line 9:

import javax.swing.*;
import java.awt.event.*;

public class miniFrame extends JDialog implements ActionListener {
JTextField textField;
JButton Submit;
JLabel playerX;
TicTacToeFrame Label = new TicTacToeFrame ();//This is line 9 referred to in the stack trace. Why does the JVM find an error on this line?

public miniFrame (TicTacToeFrame parent) {
 super (parent);//This is line 12 referred in the stack trace
 textField = new JTextField ("ENTER YOUR NAME", 20);
 Submit = new JButton ("Submit");
 playerX = new JLabel ();

 Submit.addActionListener (this);

 add (playerX);
 add (textField);
 add (Submit);
}

An alternative solution I could use is the -Xss flag to increase the size of the stack frame using the command line. But I feel as though that might be a cop-out.

Increasing the size of the stack would only delay the error.

TicTacToeFrame Label = new TicTacToeFrame ();//This is line 9 referred to in the stack trace. Why does the JVM find an error on this line?

Even though this line is not in the constructor, it is executed when a new instance of MiniFrame is constructed. It calls the constructor of TicTacToeFrame, which initializes a new Instance of MiniFrame, leading to this line being executed again. It is part of the infinite loop that is causing the stack overflow, that's why you are getting a stack overflow error on that line.

What is the point of initializing Label directly there, where it is declared? Have you tried initializing it in the constructor of MiniFrame instead? That way, you wouldn't have to initialize it with a new instance, you could initialize it with the parent instance given to the constructor. That would remove the recursion which is causing the stack overflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM