views:

223

answers:

10

I am trying to make a class in Java that builds an object of type Action which holds three ints and returns it to my other class in the array history, where history is an array of type Action. When it is called I immediately get an endless loop; hence the stack overflow.

Error - I printed 1 line, it goes on...

Exception in thread "main" java.lang.StackOverflowError
    at sudokugame.Action.<init>(Action.java:7)

Class:

public class Action {

    Action a;
    public Action(int i, int o, int p){
      a = new Action(i,o,p);
    }

    public void   setAction(int n, int b, int c){

    }

    public Action  getAction(){
        return a;
    }
}
+13  A: 

Your constructor calls itself recursively forever. A sure way to overflow a stack :)

public Action(int i, int o, int p){
    //why do you do this?
    a = new Action(i,o,p);
}

Maybe what you really wanted to do was just store i,o and p in the class instance?

public class Action {

  int i;
  int o;
  int p;

  public Action(int i, int o, int p){
    this.i = i;
    this.o = o;
    this.p = p;
  }

  ...
}

(edit: see other answer for fuller example)

Paul Dixon
A: 

Your instantiating the class within itself.

Jason Miesionczek
Why would this be a problem? The recursion is the problem here.
David M
+2  A: 

The problem is here:

a = new Action(i,o,p);

You are invoking the constructor again for a new instance inside the constructor of the class itself. Since each subsequent call to the constructor will create a new constructor call there is no way for the stack to unwind, therefore creating a stack overflow.

Andrew Hare
A: 

Does your Action class really need to hold on to a reference to another Action?

Action a;

Specifically, is this line necessary? What are you using it for?

Jack Leow
+3  A: 

Try doing it this way:

public class Action {

  int i;
  int o;
  int p;

  public Action(int i, int o, int p){
    this.i = i;
    this.o = o;
    this.p = p;
  }

  public void setAction(int n, int b, int c){
    this.i = i;
    this.o = o;
    this.p = p;
  }

  public Action getAction(){
    return this;
  }
}
Adrian Godong
A: 

sure your constructor has recursive call.

nilesh
A: 

Read Sun's tutorial on constructors

Stephen Denne
A: 

Thanks for replies, yes it seems obvious now it is pointed out :). I implemented the solution and the stackOverflowError has stopped. However i hadn't achieved what i was hoping. I need a history log for my Sudoku game where history is an array of objects and each object holds; number, box position and boxCount.

I am trying to do this by building an equivalent of a multi-dimensional array, by building and object Action which holds 3 ints. Then once built i want to store it in an array of type action. So i can access these 3 ints at position p in the Action array.

I was hoping to access the contents as such for backtracking.

a=history[p]; x=a.i; y=a.o; z=a.p;

A: 

The Command design pattern is one way to approach a situation where you want to have a history of actions taken so they can be undone, etc.

Brian T. Grant
A: 

Constructor have recursive call.

Silent Warrior