stack overflow - What does StackOverflowError mean in Java? What is its fix? -
i'm encountering following error:
exception in thread "main" java.lang.stackoverflowerror @ account.draw(account.java:47)
this relevant section of code:
public double draw(double c) { if (c > 0) { return c; } else if (c < 0 && c > accbalance) { accbalance=-c; return accbalance; } return draw(c); }
how can fix this?
in code, if c == 0
, or c <= accbalance
, keep on recursing method same value of c
. so, go infinite recursion, filling stack.
for each method invocation, stack frame allocated stack. code end allocating complete stack memory.
so, e.g, if call method first time c = 0
, how stack grows:
draw(0) draw(0) draw(0) draw(0) .. on
you keep on passing 0
argument, doesn't satisfy of base cases.
as how solve this, don't have enough context find out should go in place of return draw(c);
. shouldn't there. perhaps return draw(++c);
?? can guess.
see also:
Comments
Post a Comment