0

I come from C++ and I am starting with Java. I am having some trouble understanding Arrays. Here is my problem:

I am developing the logic of a Minesweeper clone. The grid is represented as a 2D Array of Boxes (a very simple class that I made, it just contains two booleans -mine and neutralizer- with their proper getters and setters). I keep getting a NullPointerException when I set the mines. The error apears in the lines I call grid. Here is code of my setMines method:

private void setMines()
{
    int i;
    for (i = 0; i < mines; i++)
    {
        Random row, col;

        row = new Random();
        col = new Random();

        if (grid[row.nextInt(rows)][col.nextInt(cols)].getMine())
           i--; //mine is already set
        else
           grid[row.nextInt(rows)][col.nextInt(cols)].setMine(true);
    }
}

Rows and cols are private ints of my Minesweeper Class. The are initialized in the constructor. Also y initialized grid as a 2D Array the following way:

grid = new Box[rows][cols]

Thank you for your help in advance! Greetings from Spain!

jpuriol
  • 362
  • 2
  • 14

0 Answers0