-3

Possible Duplicate:
What is a stack overflow error?

What does it mean when there is a StackOverflowError in Java?

java.lang.StackOverflowError: null
Community
  • 1
  • 1
Sean
  • 31
  • 1
  • 3
  • 1
    post some code and the full Stack-Trace. – Lukas Knuth Aug 02 '11 at 16:52
  • 10
    It means that the Stack overflowed. – jjnguy Aug 02 '11 at 16:52
  • 2
    This is commonly available information. Please Google it. – Jim Aug 02 '11 at 16:53
  • Stack overflow errors are usually cause by method that are recursive. These method call themselves too many times until the system/program runs out of memory. They are also caused by loops that run infinitely. Can you post the code where the Error occurs? – Hunter McMillen Aug 02 '11 at 16:53
  • 3
    @Hunter, infinite loops do not generally cause stack overflows. – Matthew Flaschen Aug 02 '11 at 16:54
  • @Matthew, Yes it isn't very common, but still possible. I was trying to give a broad comment since there isn't very much information. – Hunter McMillen Aug 02 '11 at 16:55
  • 1
    Posting on a site called Stack Overflow asking what a stack overflow is... – BoltClock Aug 02 '11 at 16:58
  • @Jim Sorry, I meant how do you fix it. I did Google it, but couldn't really find anything on how to fix it. – Sean Aug 02 '11 at 17:02
  • @Hunter, Matt's right (hope I can call you Matt), looping doesn't impact the Java stack; entering methods impacts the Java stack. He's entered into too many nested methods, he hasn't gone through a loop too many times. – Edwin Buck Aug 02 '11 at 17:03
  • @Edwin, Matt's fine. @Hunter, the only way I can think that an infinite loop would cause a stack overflow is if you did something weird like call `alloca` repeatedly. – Matthew Flaschen Aug 02 '11 at 17:07
  • @Edwin, I was listing possible causes of StackOverflowError, and since you can enter methods inside the body of a loop, you can affect the stack. – Hunter McMillen Aug 02 '11 at 17:09
  • `public class CauseOverflow { public static void main(String[] args) { while(true) { new CauseOverflow().A(); } } public void A() { B(); } public void B() { A(); } }` compile and run this if youd like – Hunter McMillen Aug 02 '11 at 17:12
  • Hunter, the interperter stack grows with each entered method and shrinks with each exited method, any other operation might affect values within the same "frame" on the stack, but it won't affect the stack depth. How do you write a loop where you enter methods without exiting them and manage to make it to the "next" iteration through the loop? Generally, you can't, but you can easily do so with recursive nested methods. – Edwin Buck Aug 02 '11 at 17:13
  • @Edwin, by making a logic error, one I did on purpose in my example above – Hunter McMillen Aug 02 '11 at 17:20
  • 1
    First the loop had nothing to do with it, it's the recursively nested constructors that blew your stack. Constructors are a type of specialized methods, so you actually proved Matt's point. Remove the while loop and the stack will still overflow. – Edwin Buck Aug 02 '11 at 17:22
  • @Edwin, actually, the constructor is not recursive, but otherwise your comment's right. Without the loop, the mutual recursion of `A` and `B` overflow the stack. – Matthew Flaschen Aug 03 '11 at 22:36
  • @Matthew, A is fully constructed until you exit it's constructor block. So A needs to construct a B. B is not fully constructed until you exit it's constructor block, so B needs to construct an A before it can be constructed. The second nested A is not fully constructed until it leaves it's constructor block, so the second nested A must construct a second nested B. The second nested B is not fully constructed until it leaves it's constructor block, so the second nested B must construct a third nested A. How is this not recursive? – Edwin Buck Aug 05 '11 at 14:59
  • @Edwin, maybe he changed it (I do see a pencil). But `A` and `B` are just methods. The only class is `CauseOverflow`, and it has a default constructor. So it *is* recursive, but the recursion happens between two methods after the constructor is done. – Matthew Flaschen Aug 05 '11 at 22:32

4 Answers4

4

According to the Java API Documentations for the StackOverflowError class, this error is thrown "when a stack overflow occurs because an application recurses too deeply".

Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
2

Usually means there is a recursive function where the end condition never happens. It runs, filling the stack, until you get a StackOverflowError.

antlersoft
  • 14,636
  • 4
  • 35
  • 55
2

It means that you have pushed too many frames onto the Java interpreter stack. The JVM can handle a depth of nested functions that goes pretty deep, but sooner or later you need to draw the line in the sand and say, "if you nest things deeper than this, your program is probably misbehaving". You crossed that line.

See if you're using any recursive function calls, then rewrite them in their looping equivalents (if necessary). Unnecessarily recursive functions are 90% of the reasons you throw a stack overflow exception. Also, keep in mind that Java doesn't (yet) optimize tail end recursion (which is how other environments avoid stack overflows / runaway stack growth).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
1

It means that the stack (the way the system keeps track of what has been executed) has overflowed (more was attempted to put on in than was allowed). This frequently means that you've got a recursive operation that's uncontrollably calling itself.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117