0

Hi I have this stack overflow error in Java while running a test to check if my boardSlots are empty.

public class Board {

private BoardSpot a1;
private BoardSpot a2;
private BoardSpot a3;

private BoardSpot b1;
private BoardSpot b2;
private BoardSpot b3;

private BoardSpot c1;
private BoardSpot c2;
private BoardSpot c3;


public Board(){
    a1 = new BoardSpot();
    a2 = new BoardSpot();
    a3 = new BoardSpot();

    b1 = new BoardSpot();
    b2 = new BoardSpot();
    b3 = new BoardSpot();

    c1 = new BoardSpot();
    c2 = new BoardSpot();
    c3 = new BoardSpot();
}

public class BoardSpot extends Board { //Describes a single square in tic tac toe game

private String filledWith;



public BoardSpot() {
    this.filledWith = "";

}

I originally had filledWith as type Character and I thought changing it to type String ("") would prevent the error. It did not.

While debugging, the error occurs when public Board() calls new BoardSpot(). Then for some reason public BoardSpot() does not call for this.filledWith = ""; It simply loops back to public Board().

This might be a silly question but I was wondering what caused this issue? I am trying to make a simple tic tac toe game, if you have any recommendations that might make my life easier please do say!

Wilson Cao
  • 11
  • 1
  • 1
    Why does `Boardspot` extend `Board`? – Sotirios Delimanolis Jul 29 '16 at 04:32
  • I don't think `BoardSpot` should extend `Board`. It should be a POJO – Sweeper Jul 29 '16 at 04:32
  • Concurred with the two above. `BoardSpot` is not a kind of `Board`, therefore, it should not extend it. Just a double check — **what exactly is the error?** — in case you're referring to the site and not a stack overflow. – ifly6 Jul 29 '16 at 04:33
  • 1
    If it is a stack overflow error, it is cause by the fact that when you call the `BoardSpot` constructor, it also implicitly calls the `Board` constructor, which calls a new `BoardSpot` constructor... and so on... – ifly6 Jul 29 '16 at 04:36
  • Yeah it was a stack overflow error. Changing it to a POJO worked. @ifly6 where can I learn more about how extends implicitly calls the extended constructor? – Wilson Cao Jul 29 '16 at 04:41
  • BoardSpot should not extend Board :). Board should be composed of BoardSpots like the following: `public class Board { BoardSpot[] boadSpots = new BoardSpot[9]; // Board contains 9 BoardSpots. } public class BoardSpot { } ` – Developer Marius Žilėnas Jul 29 '16 at 04:41

0 Answers0