如何解决此堆栈溢出错误?

时间:2019-09-09 20:36:39

标签: java stack-overflow cyclic-reference cyclic-dependency infinite-recursion

当我尝试运行TicTacToe程序时,我正在努力解决此StackOverFlow异常。

通过阅读堆栈跟踪信息,我知道StackOverFlow错误是循环关系的结果。这是因为当我实例化miniFrame构造函数中的TicTacToeFrame类时,一个TicTacToeFrame对象被用作miniFrame构造函数的参数。结果,这将导致无限递归。

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的构造函数实例化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);
}

这是miniFrame构造函数,它以TicTacToeFrame对象作为参数。但是我不明白为什么JVM在第9行发现一个StackOverFlow错误:

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);
}

我可以使用的替代解决方案是-Xss标志,以使用命令行增加堆栈帧的大小。但是我觉得那可能是个解决方案。

1 个答案:

答案 0 :(得分:1)

增加堆栈的大小只会延迟错误。

  

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

即使此行不在构造函数中,它也会在构造MiniFrame的新实例时执行。它调用TicTacToeFrame的构造函数,该构造函数初始化MiniFrame的新实例,从而导致该行再次被执行。这是导致堆栈溢出的无限循环的一部分,这就是为什么在那条线上出现堆栈溢出错误的原因。

直接在此处声明的Label进行初始化有什么意义?您是否尝试过在MiniFrame的构造函数中对其进行初始化?这样,您就不必使用新实例对其进行初始化,而可以使用赋予构造函数的parent实例对其进行初始化。那将消除导致堆栈溢出的递归。