Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Stack Overflow after instantiating new object

Hello, I am writing a simple game of Pong, my ultimate goal here is to allow for more than one ball to be put on the screen at once and move separately from the other balls. To accomplish this, I made a "PongBall" class with a constructor that locates the initial position of the ball in the middle of the screen. When I try to instantiate the class using:

PongBall ball_1 = new PongBall();

my IDE, Eclipse, gives me the following error:

UIDefaults.getUI() failed: createUI() failed for PongBall[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=] java.lang.reflect.InvocationTargetException
java.lang.Error

followed by hundreds of lines of errors pointing to my constructor in the PongBall class,

at PongBall.(PongBall.java:3)
at PongPanel.(PongPanel.java:19) //the instantiation
at PongBall.(PongBall.java:3)
at PongPanel.(PongPanel.java:19) //the instantiation...

which repeat hundreds of times.

There is also an error,

Exception in thread "main" java.lang.StackOverflowError
at java.lang.reflect.InvocationTargetException.(InvocationTargetException.java:54)
...
...

I looked this error report up on google, and believe it is some kind of recursion problem.

My PongBall class looks like this:

public class PongBall extends PongPanel {

public PongBall() { //constructor, start the ball in the middle
ballX = getWidth()/2;
ballY = getHeight()/2;
}

private int ballX;
private int ballY;

public int getX() {return ballX;};
public void incX() {ballX++;};
public void decX() {ballX--;};
public void setX(int newX) {ballX = newX;};

public int getY() {return ballY;};
public void incY() {ballY++;};
public void decY() {ballY--;};
public void setY(int newY) {ballY = newY;};
}

I am trying to instantiate the class using:

PongBall ball1 = new PongBall();

this line of code is inside the constructor of the class PongBall extends, PongPanel.

I have also tried modifying the constructor by changing:

public PongBall() { //constructor, start the ball in the middle
ballX = getWidth()/2;
ballY = getHeight()/2;
}

to

public PongBall() { //constructor, start the ball in the middle
ballX = super.getWidth()/2;
ballY = super.getHeight()/2;
}

to see if the subroutines inside the constructor rely on the PongPanel class to operate, I get the same result.

I very much appreciate your time in reading this and any replies as to what may be causing the problem. If there is anything I should add to make the problem description/error reporting clear, I will be more than happy to do so.

Your rating: None

Stack Overflow Between Parent and Child

DCC1,

The lines you included in your post give you the best clue when it comes to a StackOverflowError:

at PongBall.(PongBall.java:3)
at PongPanel.(PongPanel.java:19) //the instantiation
at PongBall.(PongBall.java:3)
at PongPanel.(PongPanel.java:19) //the instantiation...

As those lines indicate, there is a recursive problem related to line 3 of PongBall.java and line 19 of PongPanel.java. Whatever is on line 3 of PongBall.java (looks like the constructor) is calling line 19 of PongPanel and then whatever is on line 19 of PongPanel is calling line 3 of PongBall. They continually cyclically call each other until the Stack overflows.

Although you don't show the code for PongPanel, my guess is that line 19 of PongPanel instantiates PongBall with something like new PongBall(). Then PongBall's constructor tries to instantiate its parent PongPanel implicitly which returns a call to PongBall which calls its parent PongPanel and on and on. If PongBall does not need to extend PongPanel, an easy way to fix this problem would be to remove the extends PongPanel declaration in PongBall.

Dustin

Hi Could you post the code

Hi

Could you post the code for your PongPanel class too?

Thanks

Sorry for the late reply, I

Sorry for the late reply, I waited for a couple days with no response and stopped checking.

You are exactly right though - I removed the extension of the PongPanel class in the PongBall class and it works as expected.

Thanks for the reply, I appreciate you taking the time to help.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.