user6862532
user6862532

Reputation:

Method not seen when called from a 2d array of objects (java)?

I'm making a minesweeper game in java, and I have declared a 2d array of Tile objects as the game board. 2d array board is declared as an instance variable, then filled with Tile objects once the user passes a size (4x4 to 10x10, inclusive). I then attempt to call a method on a specific object from another method in class GameBoard using the format board[a][b].setMarked(true). I receive the error "cannot find symbol - method setMarked(boolean)". I'm confused as to how GameBoard cannot see the method in Tile, since it was declared as public and I can call it from a non-array object. I'm assuming it has to do with the instance variables and constructors?

GameBoard class relevant code:

public class GameBoard {
    private Object[][] board;

    public GameBoard(int a) {
        board = new Object[a][a];
        for (int i=0; i<a; i++) {
            for (int j=0; j<a; j++) {
                board[i][j] = new Tile(false);
            }
        }
    }

    public void mark(int a,int b) {
        board[a][b].setMarked(true);
    }
}

Tile class relevant code:

public void setMarked(boolean m) {
    marked = m;
}

where marked is a boolean instance variable declared in Tile.

Upvotes: 0

Views: 69

Answers (1)

Dmytro Grynets
Dmytro Grynets

Reputation: 953

You have array of objects, and Object have no method setMarked(boolean m), think about changing it to array of Tiles

Upvotes: 1

Related Questions