dimanche 4 mai 2014

Java - classe ajouter méthode lève StackOverflowError sur déclaration package - Stack Overflow


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.




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.





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.




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.



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.



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.




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.



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.


0 commentaires:

Enregistrer un commentaire