d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iso Help With An Error > Java.lang.stackoverflowerror
Add Reply New Topic New Poll
Member
Posts: 14,592
Joined: Dec 11 2007
Gold: 7,978.50
Nov 9 2013 02:37pm
Heya ;)
I've been studying programming in java for 2 months and we're supposed to make the game Asteroids using StdDraw (http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html).
I've pretty much completed the program and it's working very well expect for the fact that it crashes when I'm around 4-6 mins in the game.

As a newbie I'm not 100% sure how its best to do this but I ended up building it around using methods that use other methods and those methods often use the original one etc. so I guess you could say it's an endless loop.

Here is the error:

Exception in thread "main" java.lang.StackOverflowError
at java.security.AccessControlContext.optimize(AccessControlContext.java:394)
at java.security.AccessController.getContext(AccessController.java:501)
at java.awt.AWTEvent.<init>(AWTEvent.java:106)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:224)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:188)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:150)
at javax.swing.RepaintManager.scheduleProcessingRunnable(RepaintManager.java:1380)
at javax.swing.RepaintManager.addDirtyRegion0(RepaintManager.java:434)
at javax.swing.RepaintManager.addDirtyRegion(RepaintManager.java:474)
at javax.swing.JFrame.repaint(JFrame.java:777)
at java.awt.Component.repaint(Component.java:3286)
at StdDraw.draw(StdDraw.java:842)
at StdDraw.show(StdDraw.java:820)
at Game.updateGame(Game.java:107)
at Game.printGame(Game.java:95)
at Game.updateGame(Game.java:108)
at Game.printGame(Game.java:95)
at Game.updateGame(Game.java:108)
..............
...............(like 500 times)
...............
at Game.printGame(Game.java:95)
at Game.updateGame(Game.java:108)


Edit: I also seem to get this sometimes
Exception in thread "main" java.lang.StackOverflowError
at java.util.EventObject.<init>(EventObject.java:54)
at java.awt.AWTEvent.<init>(AWTEvent.java:337)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:224)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:188)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:150)
at javax.swing.RepaintManager.scheduleProcessingRunnable(RepaintManager.java:1380)
at javax.swing.RepaintManager.addDirtyRegion0(RepaintManager.java:434)
at javax.swing.RepaintManager.addDirtyRegion(RepaintManager.java:474)
at javax.swing.JFrame.repaint(JFrame.java:777)
at java.awt.Component.repaint(Component.java:3286)
at StdDraw.draw(StdDraw.java:842)
at StdDraw.show(StdDraw.java:820)
at Game.gameLoop(Game.java:107)
at Game.printGame(Game.java:95)
........................
........................
.........................
at Game.printGame(Game.java:95)
at Game.gameLoop(Game.java:108)

edit: Sometimes I get this
Exception in thread "main" java.lang.StackOverflowError
at java.util.EventObject.<init>(EventObject.java:54)

I googled this, found out that it's because of the endless loops but I have no idea how to fix this. I guess java has some kinda endless loop security, right? Why am I not allowed to loop this forever? :/
Can I change the amount of times my program can loop so I can at least have 15+min of play time. :/
Is perhaps the amount of memory it's taking? I monitored task manager while playing and it was pretty stable at ~140-160K

This post was edited by Molek on Nov 9 2013 02:53pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 9 2013 03:01pm
Sounds like you have something recursive going on. post your code.

at Game.updateGame(Game.java:107)
at Game.printGame(Game.java:95)
at Game.updateGame(Game.java:108)
at Game.printGame(Game.java:95)
at Game.updateGame(Game.java:108)

This post was edited by carteblanche on Nov 9 2013 03:02pm
Member
Posts: 14,592
Joined: Dec 11 2007
Gold: 7,978.50
Nov 9 2013 03:21pm
Quote (carteblanche @ Nov 9 2013 09:01pm)
Sounds like you have something recursive going on. post your code.

at Game.updateGame(Game.java:107)
at Game.printGame(Game.java:95)
at Game.updateGame(Game.java:108)
at Game.printGame(Game.java:95)
at Game.updateGame(Game.java:108)


Well in order to make the game move around I have to make the game run endlessly
The program is in total about 700 lines so I'll just post how the game basically functions.


Code
public static void main(String args[])
{
printGame();
}
public void printGame()
{
//clears screen
//print everything on screen
updategame();
}
public void updateGame()
{
inputCheck();
statusCheck();
updateLocations();
collisionCheck();
//...and other similar methods
printGame(); // this being executed forever should be the cause of the error
}


This post was edited by Molek on Nov 9 2013 03:24pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 9 2013 03:27pm
Quote (Molek @ Nov 9 2013 05:21pm)
Well in order to make the game move around I have to make the game run endlessly
The program is in total about 700 lines so I'll just post how the game basically functions.


Code
public static void main(String args[])
{
  printGame();
}
public void printGame()
{
  //clears screen
  //print everything on screen
  updategame();
}
public void updateGame()
{
  inputCheck();
  statusCheck();
  updateLocations();
  collisionCheck();
  //...and other similar methods
  printGame(); // this being executed forever should be the cause of the error
}


can you explain why they have to call each other? that leads to your infinite recursion problem since i see no way the recursion terminates. so each function never actually terminates, so you have lots and lots of unending functions.

why not just put the printGame() code inside the updateGame() block and use a loop (and optionally a timer/thread.sleep) to stay in updateGame() forever? this way each time updateGame() is invoked, it finishes and goes off the stack.

eg:

Code
public static void main(String args[])
{
while (true){
updateGame();
// optionally sleep
}
}
public void updateGame()
{
inputCheck();
statusCheck();
updateLocations();
collisionCheck();
//...and other similar methods

// everything from printGame goes here, or on the top. whichever works for your logic
//clears screen
//print everything on screen

}


This post was edited by carteblanche on Nov 9 2013 03:31pm
Member
Posts: 14,592
Joined: Dec 11 2007
Gold: 7,978.50
Nov 9 2013 03:42pm
Quote (carteblanche @ Nov 9 2013 09:27pm)
can you explain why they have to call each other? that leads to your infinite recursion problem since i see no way the recursion terminates. so each function never actually terminates, so you have lots and lots of unending functions.

why not just put the printGame() code inside the updateGame() block and use a loop (and optionally a timer/thread.sleep) to stay in updateGame() forever? this way each time updateGame() is invoked, it finishes and goes off the stack.

eg:


Oh you're right. Hmm i thought having something in while(true){} would be similar to calling functions over and over again. I guess it becomes a problem since I never actually finish the functions. I'm gona have a lot of things to fix, lol.
Thanks man
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll