0

When I run my program, I get a StackOverflowError.

What is incorrect in my approach? I'm not sure how to pass it in without causing it to fail to compile. The initDialog and initComponents methods are just for the rest of the program to create the interface

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

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

Stacktrace:

Exception in thread "main" Exception in thread "main" java.lang.StackOverflowError
    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)
W1492046
  • 31
  • 9
  • 1
    @Idos It's got to be `diceGUI.initDialog()` :-) – Sergey Kalinichenko Dec 12 '15 at 14:44
  • And where do you initialize `diceGUI`? – Tom Dec 12 '15 at 14:44
  • Where is `diceGUI` variable set? – Sergey Kalinichenko Dec 12 '15 at 14:44
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – David Dec 12 '15 at 14:49
  • Updated my question, thanks for helping me with the NPE – W1492046 Dec 12 '15 at 15:23
  • I highly doubt that this code alone produces any exception... Please post a relevant snippet reproducing the exception – Dici Dec 12 '15 at 16:13
  • Then you have a new question, then create a new one. Changing the _complete_ question is very disliked here. – Tom Dec 12 '15 at 16:28
  • @Tom, apologies for changing the complete question Dici, updated the code – W1492046 Dec 12 '15 at 16:32
  • The stacktrace is also useful when debugging an exception – Dici Dec 12 '15 at 16:38
  • Also, the second class is not used in your main so irrelevant here What is `initDialog` ? The code of this method is missing – Dici Dec 12 '15 at 16:41
  • The syacktrace is interesting, but does not correspond to your code Could you provide the actual code so that we can test ourselves ? – Dici Dec 12 '15 at 16:43
  • The second class is where the issue comes from, code has been updated with the initDialog method – W1492046 Dec 12 '15 at 16:43
  • You don't show how `sch` is initialized, which is why I could not see your second class was actually used. To me this shows you don't know where to look when debugging, hence you don't know what to show for us to debug. – Dici Dec 12 '15 at 16:45

2 Answers2

1

You did not help us much with your question, so please improve it. The only reason I could see your error is because you sent me the whole code on Facebook, this is not normal. A question on SO should contain all the information for anybody to find the issue.

You are constructing an instance of SwingComponentsHandler for every instance of DiceGUI, but you are also creating an instance of DiceGUI for every instance of SwingComponentsHandler. Do you see the problem ?

Let's draw a dependency graph. A >> B means A needs to construct B to be constructed. We have DiceGUI >> SwingComponentsHandler and SwingComponentsHandler >> DiceGUI, hence, because a dependency is transitive, we have DiceGUI >> SwingComponentsHandler >> DiceGUI >> SwingComponentsHandler >> DiceGUI....

This never ends, this is an infinite recursion. A StackOverflowError is ALWAYS the sign of a very deep or infinite recursion. If you are not writing a recursive function, the debugging is easy. Just look at the stacktrace, it should contain alternatively a line A and a line B which call each other. It's like an NPE, you can debug this on your own in a few minutes just by looking at the stacktrace.

Dici
  • 25,226
  • 7
  • 41
  • 82
  • You were right, I should have only declared it where it was needed. I shall update the code properly when I get the chance – W1492046 Dec 12 '15 at 17:08
0

You have an infinite recursion somewhere in your code but not visible in that example. I tend to believe you instantiate DiceGUI() or initialize repetitively somewhere.

Make sure to use new DiceGUI() and its methods correctly.

Guillaume F.
  • 5,905
  • 2
  • 31
  • 59
  • What you say is correct but not an answer. The answer, as you said, is outside the snippet in the question. – Dici Dec 12 '15 at 16:23