Advertisement

stackOverflowError help plz!

Started by January 01, 2005 03:35 PM
5 comments, last by BangorRipper 19 years, 10 months ago
hi guys, happy new year to you all. just trying to load my ship model after i have loaded all of my alien models in space invaders using the same method but in a different class but i'm getting a stackOverflowError. Now is there some way that i am supposed to dispose of the stack after i have finished loadiong my aliens or something. below is a portion of the code that i am using in both the alien class and the ship class to store the models:- float[] floatCoords = new float[coords.size()]; for (int x = 0;x<coords.size();x++) { floatCoords[x] = Float.parseFloat((String) coords.elementAt(x)); } shipData = BufferUtils.createFloatBuffer(coords.size()); shipData.put(floatCoords); shipData.flip(); should i maybe call it floatCoords1 in the ship class or something???!! thanks everyone :)
it could be this line
for (int x = 0;x<coords.size();x++)
try
for (unsigned long x = 0;x<coords.size();x++)

If coords.size() is larger than what an integer can take the whole things goes pooey.
Advertisement
lc_overlord: Thats Java code, so theres no such thing as 'unsigned long'. :)

BangorRipper: Stack overflow in Java almost always means you've got yourself jammed into infinite recursion in some way, since no actual objects get created on the stack (unlike C++). Equally, you never have to worry about cleaning up the stack either.

The code you posted doesn't look like the curprit. Post the entire stack error message (and stack trace with it) and the code it occured from.

OT.
here is the end of the trace writes and the error:-
xAlien = 20.0, yAlien = 0.0, zAlien = 3.0
xAlien = 20.0, yAlien = 0.0, zAlien = 6.0
xAlien = 24.0, yAlien = 0.0, zAlien = 0.0
xAlien = 24.0, yAlien = 0.0, zAlien = 3.0
xAlien = 24.0, yAlien = 0.0, zAlien = 6.0
xShip = -1.0, yShip = 0.0, zShip = 10.0
Exception in thread "main" java.lang.StackOverflowError
Press any key to continue...

here is the code that seems to be the problem:-
/**
* Render a collection of aliens to the screen using the GL commands
* and the buffer we've read from the RAW file.
*/
private void renderShip()
{
if (shipIsAlive) {
GL11.glPushMatrix();
GL11.glTranslatef(xShip,yShip,zShip); //move forward
System.out.println("hello");
renderShip();
GL11.glPopMatrix();
}
}

if i put a trace write in here i see a list of hello's nad then the eroor, if i put the hello after the renderShip() then no hello's are displayed
hope someone can spot the problem now
thanks
Quote: Original post by BangorRipper
here is the end of the trace writes and the error:-
xAlien = 20.0, yAlien = 0.0, zAlien = 3.0
xAlien = 20.0, yAlien = 0.0, zAlien = 6.0
xAlien = 24.0, yAlien = 0.0, zAlien = 0.0
xAlien = 24.0, yAlien = 0.0, zAlien = 3.0
xAlien = 24.0, yAlien = 0.0, zAlien = 6.0
xShip = -1.0, yShip = 0.0, zShip = 10.0
Exception in thread "main" java.lang.StackOverflowError
Press any key to continue...

You still haven't posted the proper stack trace - thats the one with the list of methods called (and line numbers) when the exception happened. That should appear just before the "Press any key" text. If it doesn't, then you've got a whacking great big try...catch(Exception) block around your code (doing nothing!) which is just plain wrong.

Fortunatly its obvious from your code whats wrong. I've already given you the hint you need (infinite recursion!) if you still can't figure out whats wrong with your code then you're beyond help.

OT.
You have an infinite recursive call for any ships that are still alive. Try following the program flow through yourself on paper. Any time you have a recursive method you must have a base case. In this case I can't see why you are using recursion. Why are you calling renderShip from within itself? What are you trying to achieve?

(Just to be clear, the following is the line at fault:)
/*** Render a collection of aliens to the screen using the GL commands* and the buffer we've read from the RAW file.*/private void renderShip() {	if (shipIsAlive)	{		GL11.glPushMatrix();		GL11.glTranslatef(xShip,yShip,zShip); //move forward		System.out.println("hello");		renderShip();		GL11.glPopMatrix();	}}

Enigma
Advertisement
thanks for the replies guys, not what i thought though!
i wasnt meant to call the same method again, i was meant to call a different method and not the same one, thanks anyway :)

This topic is closed to new replies.

Advertisement