• Post Reply Bookmark Topic Watch Topic
  • New Topic

StackOverFlow error: null when trying to use iterative method  RSS feed

 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, so essentially what I am trying to do is get my compiler to add the sum of my digits through both iteratively and recursively. However, when I try running my program, I get a StackOverFlow error: null at my iterative method. Im pretty sure that this is how my method should work. Am I missing something? Here is my code:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You "iterative" solution, even though it has "iterative" in the method name, is really a recursive method with no termination condition.

Henry
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could I change it to be iterative the if it is actually recursive?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would changing the while loop to a for loop like:



make it iterative?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would replace line 32 with something that doesn't call the same method again.  That line makes the method recursive.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright I changed line 32. Is this better?



The change is now line 34.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:Alright I changed line 32. Is this better?

I might clean and tighten it up a little, like this
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu, that looks a lot cleaner!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!