I am working on a game for the past month or so and no matter what I do, I almost always get a StackOverflowError
. This is what it says in the trace :
Exception in thread "main" java.lang.StackOverflowError
at retroscroller.Player.<init>
at retroscroller.Hole.<init>
The error occurs at these lines : (Player Class)
Hole hl = new Hole();
(In the hole class):
Player ch = new Player();
Do I get the error because both classes are extending each other and both are superclasses ?
Do I get the error because both classes are extending each other and both are superclasses ?
No, that would be impossible at the language level. It wouldn't compile.
However, if creating a Player requires creating a Hole, which requires creating a Player... then you've got a problem. Debug into the constructor and you should see what's going on. (It's important to get familiar with a debugger... even though I prefer unit testing over debugging most of the time, sometimes you can save a lot of time by debugging...)
Consider making one of the constructors pass this
to the other, or preferrably remove the cyclic dependency in the first place. (Do the two objects really need to know about each other?)
See more on this question at Stackoverflow