Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in mine sweeper program

So I'm a computer science student and a fledgling programmer in Java. Someone asked me to help them on an assignment where they have to create a pretty basic minesweeper program. This program doesn't utilize flagging mines at all, but aside from that it is functionally the same as any other mine sweeper game.

I'm running into a NullPointerException when I try to run the program. I've researched what this could mean and now know that this should really be a NoObjectException or DereferenceException, but I'm still no closer to solving the issue.

This exception arises when the makeField method of the Tile class is called. Also, I'm really trying to wrap my head around proper inheritance, static vs. non-static, public vs. private, and how all of these inter-relate, so I'm sorry if this is a total noob question.

So, I have a main file, a Tile super class, and two sub-classes of the tile class- Bomb and Flat. Bomb is a tile with a bomb in it, and Flat is any tile that isn't a bomb.

public class MineSweeperMain{
public static void main(String[] args)
{
    Scanner kybd = new Scanner(System.in);
    int dimension;
    Tile[][] gameBoard;

    System.out.print("Enter the dimension of the board you would like to play on:\t");
    dimension = kybd.nextInt();

    gameBoard = Tile.makeField(dimension);
    Tile.printField(gameBoard, dimension);
}

}

//

public class Tile {

static Random rand = new Random();

boolean isBomb;
boolean isRevealed;
int posX, posY;
int noOfAdjacentMines;

public Tile()
{
    isRevealed = false;
}

public static int detectMines(Tile[][] board, int dimensions)
{
    int detectedMines = 0;
    for(int i = 0; i < dimensions; i++)
    {
        for(int j = 0; j < dimensions; j++)
        {
            if(board[i][j].isBomb)
                detectedMines++;
        }
    }
    return detectedMines;
}

public static Tile[][] makeField(int dimensions)
{   
    int rowOfMines = dimensions / 3;
    int randomInRow;

    Tile[][] Board = new Tile[dimensions][dimensions];

    for(int i = 0; i < dimensions; i++)
        for(int j = 0; j <= rowOfMines; j++)
        {
        randomInRow = rand.nextInt(dimensions);
        Board[i][randomInRow] = new Bomb();
        }

    for(int i = 0; i < dimensions; i++)
        for(int j = 0; j < dimensions; j++)
        {
            if(!Board[i][j].isBomb)
                Board[i][j] = new Flat();
        }
    return Board;               
}

public static void printField(Tile[][] board, int dimensions)
{
    for(int i = 0; i <= dimensions; i++)
    {
        for (int j = 0; j <= dimensions; j++)
        {
            if(i ==0)
                System.out.print(i + " ");
            else if(j == 0)
                System.out.print(j + " ");
            else
            {
                if(board[i-1][j-1].isRevealed && !board[i-1][j-1].isBomb)
                    System.out.print(board[i-1][j-1].noOfAdjacentMines + " ");  
                else
                    System.out.print("# ");
            }
        }
    }
}

}

//

public class Flat extends Tile{

public Flat()
{
    noOfAdjacentMines = 0;
    isBomb = false;
    isRevealed = false;
}
}

//

public class Bomb extends Tile{
public Bomb()
{
    isBomb = true;
    isRevealed = false;
}

}

//

like image 268
Gthoma2 Avatar asked Dec 06 '22 16:12

Gthoma2


1 Answers

Your issue is in the second loop of the makeField method I think.

When you check if(!Board[i][j].isBomb) That particular value is going to be null because you have not filled your array fully yet. There are a couple random bombs placed by the first loop, but the rest of the values are null.

I would recommend reversing your loops. First loop through everything and make the whole board out of Flats without checking anything.

Then in your second loop, you will just overwrite a couple Flats with Bombs

The other solution is just to make this tiny modification and check for null:

if(null == Board[i][j] || !Board[i][j].isBomb)

Note that if you take this solution, the null check must be FIRST. This is because of something called short-circuting.

The reason I recommend my first solution though of switching the loops is because it eliminates 1 extra comparison, not huge deal, but you never know...

like image 56
gnomed Avatar answered Dec 20 '22 11:12

gnomed