传入空字符串作为参数时出现 StackOverflowError

StackOverflowError when passing in empty string as parameter

当我 运行 我的程序时,我得到一个 WhosebugError。

我的方法有什么不正确的地方?我不确定如何在不导致编译失败的情况下传递它。 initDialog 和 initComponents 方法仅供程序的其余部分创建界面

public class DiceGUI extends JFrame {
    DiceGUI(String title) {
        super(title);
        initDialog();

        setSize(1000, 800);
        setLayout(new BorderLayout());
        add(mainPanel, BorderLayout.CENTER);

        addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            setDefaultCloseOperation(sch.closeHandler());
       }
    });
}

    public static void main(String[] args) {
        DiceGUI diceGUI = new DiceGUI("Dice Game");
        diceGUI.setVisible(true);
    }
}

    public void initDialog() {
        dPanel = new JPanel();
        dPanel.setLayout(new BoxLayout(dPanel, BoxLayout.Y_AXIS));
        JLabel invalidInput = new JLabel("");
        String[] options = {"OK"};
        dPanel.add(new JLabel("Leave blank to make target 101, enter a number below to change it"));
        dPanel.add(invalidInput);
        JTextField text = new JTextField("");
        dPanel.add(text);

        boolean flag;
        do {
            int changeGameTarget = JOptionPane.showOptionDialog(null, dPanel, "Dice Game", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            flag = sch.dialogHandler(changeGameTarget, text, invalidInput);
        } while (!flag);

        text.setText("");
    }

第二个Class

public class SwingComponentsHandler {
    private DiceGUI diceGUI = new DiceGUI("");


    public void restartHandler(JButton r) {
          r.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  String msg = "New Game?";
                  int yes = JOptionPane.showConfirmDialog(null, msg, "New Game?", JOptionPane.YES_NO_OPTION);

                  // Restart game
                  if (yes == JOptionPane.YES_OPTION) {
                      diceGUI.initDialog();
                  }
              }
          });
    }
}

堆栈跟踪:

Exception in thread "main" Exception in thread "main" java.lang.WhosebugError
    at sun.awt.X11GraphicsConfig.pGetBounds(Native Method)
    at sun.awt.X11GraphicsConfig.getBounds(X11GraphicsConfig.java:314)
    at java.awt.Window.init(Window.java:505)
    at java.awt.Window.<init>(Window.java:537)
    at java.awt.Frame.<init>(Frame.java:420)
    at javax.swing.JFrame.<init>(JFrame.java:233)
    at DiceGUI.<init>(DiceGUI.java:21)
    at SwingComponentsHandler.<init>(SwingComponentsHandler.java:11)
    at DiceGUI.<init>(DiceGUI.java:16)

您的代码中某处有无限递归,但在该示例中不可见。我倾向于相信你在某处实例化 DiceGUI() 或重复初始化。

确保正确使用 new DiceGUI() 及其方法。

您的问题对我们帮助不大,请改进。我能看到你的错误的唯一原因是你在 Facebook 上把整个代码发给我了,这是不正常的。 SO 上的问题应该包含任何人都能找到问题的所有信息。

您正在为 DiceGUI 的每个实例构建一个 SwingComponentsHandler 的实例,但您同时也在为 SwingComponentsHandler 的每个实例创建一个 DiceGUI 的实例。你看到问题了吗?

让我们画一个依赖图。 A >> B表示A需要构造B才能构造。我们有 DiceGUI >> SwingComponentsHandlerSwingComponentsHandler >> DiceGUI,因此,因为依赖是传递性的,所以我们有 DiceGUI >> SwingComponentsHandler >> DiceGUI >> SwingComponentsHandler >> DiceGUI....

这永远不会结束,这是一个无限递归。 WhosebugError 总是非常深或无限递归的标志。如果您不编写递归函数,则调试很容易。看看堆栈跟踪,它应该交替包含一行 A 和一行 B ,它们相互调用。就像一个NPE,你可以在几分钟内通过查看stacktrace自行调试。