• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to avoid stack overflow error when using recursion?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to write a chess program with the netbeans GUI creator. So far I've got the visual part of the program down; I've created a chess board that will set up on command, and with pieces that you can move anywhere on the board by clicking them twcie (once to select, once to move), and now I'm trying to add restrictions to only allow legal moves. In order to do this, I created a separate class, which I'm calling Referee, with several methods, all of which build on each other to finally determine whether or not a given move is legal (for example, one method checks if a move is legal only in terms of how different pieces are allowed to move, another determines, given a certain position, and using the first method, if either player is in check, and then a final method uses both the surface review based on move capabilites and the position review based on check to decide once and for all if a move is legal).

Anyway, the details aren't super important, but the main point is that there are several methods within this class, all of which build on each other, and thus need to be called within the class itself. However, the class also has one method that I need to call from the main class (the full review, to determine if a move is legal). In order to be able to call methods from the Referee class within the main class, and the Referee class, I created a separate object in each class (ref1 inside Referee class, and ref2 inside the main class). However, I'm now having trouble with a stack overflow error. The error comes up on the object creation in the main class; I guess what's happening is that when I create the ref2 object, it has to have its own ref1 object, which in turn has to have its own ref1 object, etc. etc., on to infinity.

public static void main(String args[])
{
Referee ref2 = new Referee(); //the stack overflow error is happening here
}
public class Referee
{
Referee ref1 = new Referee();
}

Does anyone know how to redesign my class set up such that I don't run into this error? I can't figure out a way to call the referee methods from within the referee class without causing this error, although I guess that it is possible since I have seen recursion sucessfully done before.

Help would be much appreciated.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The root cause is

So why are you constructing a new Referee instance in the Referee constructor
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what happens then is you create a Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, which is initialized by calling the Referee constructor, and that constructor creates a new Referee object, ... until the stack overflows.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In order to be able to call methods from the Referee class within the main class



Okay, you need an instance to work with from main, but why would you need a new instance inside every instance?
Why do you think you need this, which, as rightly pointed out above, is the problem spot?
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I guess I thought I needed ref1 in order to call referee methods from within other referee methods, but what all of you guys seem to be implying is that I don't. So for example, inside my method public boolean fullyReviewMove, instead of using the method reviewPosition like this:

ref1.reviewPosition(),

I will just use it like this, without the ref1:

reviewPosition()

?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you want to call a method on the same object as the current method is executing on, you just call the method, you don't need a reference to call it on.

You could call it with the "this" keyword: this.reviewPosition(). this refers to "the current object". But you can also just call reviewPosition(), you don't need to explicitly specify "this".
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright thanks a lot guys.
 
reply
    Bookmark Topic Watch Topic
  • New Topic