1

I am trying to close a window when OK button is pressed. But when I try to run the file, it gives me a StackOverflowError.

Here is my code:

public class Test1 extends javax.swing.JFrame {

Test1 t = new Test1();
/**
 * Creates new form Test1
 */

public Test1() {
    initComponents();
}

And this is the actionListener for the button.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        t.setVisible(false);
    }

This is the error message,

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
    at sun.awt.Win32GraphicsConfig.getBounds(Win32GraphicsConfig.java:222)
    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 java.awt.Frame.<init>(Frame.java:385)
    at javax.swing.JFrame.<init>(JFrame.java:189)
    at Test1.<init>(Test1.java:11)
    at Test1.<init>(Test1.java:13)
    at Test1.<init>(Test1.java:13)
    at Test1.<init>(Test1.java:13)
    at Test1.<init>(Test1.java:13)
    at Test1.<init>(Test1.java:13)
    at Test1.<init>(Test1.java:13)
    ...and so on

Test1.java:11 refers to public class Test1 extends javax.swing.JFrame and Test1.java:13 refers to Test1 t = new Test1();

What is the problem here? How can I solve it?

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
Andy
  • 29
  • 4

0 Answers0