my dog learned polymorphism*
The moose likes Beginning Java and the fly likes StackOverflowError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StackOverflowError" Watch "StackOverflowError" New topic
Author

StackOverflowError

Matthew Gates
Greenhorn

Joined: Nov 11, 2005
Posts: 3
I have a program that I am writing that allows me to enter and display data for a group of sales representatives. The program compiles, but when I attempt to enter the name of a representative, I receive a StackOverflowError. What could be causing this?
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Please post some sample code.

Wild guess is that you have an infinite loop.


My Blog: Down Home Country Coding with Scott Selikoff
Matthew Gates
Greenhorn

Joined: Nov 11, 2005
Posts: 3


[ EJFH: Added code tags to preserve formatting. ]
[ November 11, 2005: Message edited by: Ernest Friedman-Hill ]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

see anything wrong with this...

[ November 11, 2005: Message edited by: Scott Selikoff ]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

BTW, as a suggestion for debugging, I just started up Eclipse, created the class from your posted code, added a breakpoint before it asked for the representative name, then started the program in debugging mode...

after that finding where the infinite loop was trivial.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Both these routines call themselves, passing their same arguments to the new call:



When findRep(String) calls findRep(String), that second call will call findRep(String) which will call findRep(String)... eventually, the JVM will run out of stack space to keep track of all those calls that haven't returned yet, and you'll get a StackOverflowException.

Both versions of findRep need to be implemented in terms of the ArrayList, not in terms of recursive calls.



[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: StackOverflowError