1

I was am looking for a solution to this error what is written on the console is Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at sun.awt.Win32GraphicsConfig.getBounds(Native Method) at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source) at java.awt.Window.init(Unknown Source) at java.awt.Window.<init>(Unknown Source) at java.awt.Frame.<init>(Unknown Source) at java.awt.Frame.<init>(Unknown Source) at javax.swing.JFrame.<init>(Unknown Source) Here is my code First Class

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class testing extends JFrame {

private JPanel contentPane;
private testing tes = new testing();
private boolean ranOnce = false;

public testing() {
     if (ranOnce = false) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        ranOnce = true;
                        tes = new testing();
                        tes.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JLabel lblTest = new JLabel("Test");
    contentPane.add(lblTest, BorderLayout.CENTER);
}
}
}

Second Class

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Test extends JFrame {

private JPanel contentPane;


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test frame = new Test();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnTestButton = new JButton("Test Button");
    btnTestButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            new testing();
        }
    });
    btnTestButton.setBounds(114, 90, 89, 23);
    contentPane.add(btnTestButton);
}
}

I am using the eclipse plugin window builder I know the stack-overflow exception is caused by too much memory use and I thought using if(runOnce = false) it world fix it

1
  • For the future: when you have a stack overflow, you have to look for a repeating patern in the call stack. The call stack you're providing cuts off before any such pattern can be detected. Obviously, you cannot provide a complete call stack, but something longer would have been more helpful. Commented Jan 23, 2015 at 20:18

2 Answers 2

3

in the testing constructor, you are creating a new instance of testing each time you create an instance of testing, so you're recursively creating infinite number of testing instances.

Your approach to solve this problem is good, but your ranOnce boolean is a member variable, as such, there is one per testing instance, so it is always false when you hit the constructor.

You should simply make ranOnce static, so that there is only one ranOnce variable, bound to the class and not the instances.

EDIT: as stated by Sbodd, you'll also have to replace

private testing tes = new testing();

with

private testing tes;

so that it is not initialized anymore automatically at each constructor call (so that's the second reason why it ran in an infinite recursive way)

Sign up to request clarification or add additional context in comments.

5 Comments

In addition to that, testing's tes member variable is also being initialized with 'new testing()' on each call to testing's constructor - so there are two places where the infinite recursion is happening..
@Angivare I made run once static and only called new testing once at the start and i still cant get it to work could you please help me
Does anyone know how to fix this?
As stated Sbodd, you'll also have to replace private testing tes = new testing() with private testing tes. But still, I wonder what you're trying to achieve, why are you creating a testing instance insinde testing instances?
@Angivare i am trying to make it so that when you press a button it opens a new window
2

I fixed the error by removing all the runOnce stuff in the testing class and change the button clicked code to

testing s = new testing();
s.setVisible(true);
setVisible(false);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.