-2

I am producing a library for personal use; in this particular class, extending JPanel, and my code still for some random reason, produces a StackOverflowError any tips?

public class XJPanel extends JPanel
{
    static boolean isManaged = false;

    public XJPanel() {
        
    }

    public XJPanel(LayoutManager arg0) {
        super(arg0);
        isManaged = true;
    }

    public XJPanel(boolean arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }

    public XJPanel(LayoutManager arg0, boolean arg1) {
        super(arg0, arg1);
        isManaged = true;
    }
    public GridBagConstraints getConstraints()
    {
        if(isManaged = true)
        {
            return new GridBagConstraints();
        }
        throw new NullPointerException("No Layout Manager Found");
    }
    

    /*
    public XJPanel add(Component arg0)
    {
        JPanel p = getRoot();
        p.add(arg0);
        return null;
    }

    private JPanel getRoot() {
        return this;
    }*/
    

the error it reads is exactly as follows. I think I understand what a StackOverflowError is, but I'd like to know why the error message includes the package declaration -- it contains no code, so... why?

Exception in thread "main" java.lang.StackOverflowError
at javar.swing.XJPanel.getRoot(XJPanel.java:61)
at javar.swing.XJPanel.add(XJPanel.java:55)
at javar.swing.XJPanel.add(XJPanel.java:1)
...
at javar.swing.XJPanel.add(XJPanel.java:56)
at javar.swing.XJPanel.add(XJPanel.java:1)
at javar.swing.XJPanel.add(XJPanel.java:56)

the javar.swing.XJPanel.add(XJPanel.java:1) being my package declaration.

Note:

I'm sorry for being such a jerk, @SimonC I guess I had a case of the grumps, I'm well rested now and ready to shape up.

Community
  • 1
  • 1
  • possible duplicate of [What is a stack overflow error?](http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error) – Jason C Apr 05 '14 at 00:09
  • 2
    As for tips: Include relevant information in questions. It does not matter that this is a personal project. It does not matter that you are using `JFrame`s or Swing for anything. The [tag:extending-classes] tag is irrelevant. "Package declarations" are unrelated. A `StackOverflowError` is a `StackOverflowError` in any context, and the stack trace shows the information you need (including the line numbers of the actual code path). – Jason C Apr 05 '14 at 00:13
  • Please try to debug your programs before asking for help: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – SimonC Apr 05 '14 at 00:35
  • It is not very small, it is just one class in a pretty big library of Classes – PotatoPhil Apr 05 '14 at 00:42
  • 1
    Yes it is. The problem can be isolated to a very small program. – SimonC Apr 05 '14 at 00:45
  • 2
    Also, you're not going to elicit much help by coming across as arrogant in your question. If you know perfectly well what a `StackOverflowError` is, then it should be obvious to you why the name of your package is included in the stack trace! – SimonC Apr 05 '14 at 00:50
  • `at javar.swing.XJPanel.getRoot(XJPanel.java:61` => At line 61 of getRoot() of XJPanel.java in the package javar.swing.XJPanel , you have a error – spiderman Apr 05 '14 at 00:50
  • oh yes SimonC, you are right. well said.. Its like going to school after graduation! – spiderman Apr 05 '14 at 00:51
  • 1
    @PotatoPhil Please do not be rude in questions, we're helping you on our own free time. :) – Jason C Apr 05 '14 at 01:30

3 Answers3

2

You have:

public XJPanel add(Component arg0)
{
    JPanel p = getRoot();
    p.add(arg0);
    return null;
}

private JPanel getRoot() {
    return this;
}

This is, of course, infinitely recursive (as your stack trace clearly indicates, and as a quick glance at the code shows -- try running through it in your head).

See also the documentation for StackOverflowError, which states:

Thrown when a stack overflow occurs because an application recurses too deeply.

Jason C
  • 38,729
  • 14
  • 126
  • 182
2

You have implemented the add method recursive.

public XJPanel add(Component arg0) {
    JPanel p = getRoot();
    p.add(arg0);
    return null;
}

private JPanel getRoot() {
    return this;
}

is essentially equivalent to

public XJPanel add(Component arg0)
{
    this.add(arg0);
    return null;
}

Which would be unbounded infinite recursion, which would obviously give a stack overflow error.


Edit in response to the comment:

When stack overflows, the further execution is impossible, so the last methods on the stack are printed for the purpose of debugging. This prints complete name of the method including package name, class name and method name (which can be seen here).

Community
  • 1
  • 1
Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
  • I wasn't referring to that, I just wanted to know why the `StackOverflowError` was saying that it included the package declaration, in this case `package javar.swing;` – PotatoPhil Apr 05 '14 at 00:29
  • 2
    Because that's the name of the package of the last method called before the stack overflowed. – SimonC Apr 05 '14 at 00:37
1

The other questions answer why a StackOverflowError is being thrown (not randomly of course), but to answer your specific question of why your package name is included in the stack trace: because that's the package name of the last methods being called before the stack overflowed.

SimonC
  • 6,590
  • 1
  • 23
  • 40