How to dance around a NullPointerException?

195 views Asked by At

I'm trying to check whether or not a move is legal in the game Othello, using eclipse and gridworld. The first thing I do to the location is check if it is valid, but in order to check the validity location, it needs to not be null. The problem is, one of the requirements of it being a legal move is that it is null/empty/unoccupied. How do I avoid this? I have pointed out where the error is supposedly at. (Sorry if this confused anyone.)

public boolean isLegal(Location loc1)
{
    boolean isLegal = false;
    String currentColor = currentPlayer.getColor();
    int row = loc1.getRow();
    int col = loc1.getCol();
    if(board.isValid(loc1))
    {
        if(board.get(loc1) == null)
        {
            for(Location tempLoc : board.getValidAdjacentLocations(loc1))
            {
                **if(!board.get(tempLoc).equals(currentColor))**
                {
                    if((row != tempLoc.getRow()) && (col == tempLoc.getCol()))
                    {
                        //count up column
                        if(tempLoc.getRow() < row)
                        {
                            for(int i = row; i > 1;)
                            {
                                Location tempLoc2 = new Location(i-2, col);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i--;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count down column
                        else
                        {
                            for(int i = row; i < 6;)
                            {
                                Location tempLoc2 = new Location(i+2, col);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i++;
                                }
                                else
                                {
                                    i=9;
                                    isLegal = true;
                                }
                            }
                        }
                    }
                    else if(col != tempLoc.getCol() && row == tempLoc.getRow())
                    {
                        //count right row
                        if(col > tempLoc.getCol())
                        {
                            for(int i = col; i > 1;)
                            {
                                Location tempLoc2 = new Location(row, i-2);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i--;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count left row
                        else
                        {
                            for(int i = col; i < 6;)
                            {
                                Location tempLoc2 = new Location(row, i+2);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i++;
                                }
                                else
                                {
                                    i=9;
                                    isLegal = true;
                                }
                            }
                        }
                    }
                    else
                    {   //count up/right diag
                        if(row-1 == tempLoc.getRow() && col+1 == tempLoc.getCol())
                        {
                            int j = col;
                            for(int i = row; i > 1;)
                            {
                                Location tempLoc2 = new Location(i-1, j+1);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i--;
                                    j++;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count down/left diag
                        else if(row+1 == tempLoc.getRow() && col-1 == tempLoc.getCol())
                        {
                            int i = row;
                            for(int j = col; j > 1;)
                            {
                                Location tempLoc2 = new Location(i+1, j-1);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i++;
                                    j--;
                                }
                                else
                                {
                                    i=9;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count up/left diag
                        else if(row-1 == tempLoc.getRow() && col-1 == tempLoc.getCol())
                        {
                            int j = col;
                            for(int i = row; i > 1;)
                            {
                                Location tempLoc2 = new Location(i-1, j-1);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i--;
                                    j--;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count down/right diag
                        else
                        {
                            int j = col;
                            for(int i = row; i > 6;)
                            {
                                Location tempLoc2 = new Location(i+1, j+1);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i++;
                                    j++;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                    }
                }
            }
        }
    }
    return isLegal;
}
3

There are 3 answers

3
calebds On

One solution is to change your design so that no location is ever null.

You seem to have equated null with "unoccupied" or "empty". Instead create all positions first (there aren't many of them on an Othello board) and initialize them all with boolean occupied = false or an equivalent member variable. Then you'd have:

if ( !board.get(loc1).isOccupied() ) { /*stuff*/ }

instead of a null check.

This is better object oriented design because an empty location is still a location, and should be manipulable.

0
Adam Stelmaszczyk On

You shouldn't use null as a part of your logic.
null it's not a state, it is a symbol that there is no state.
You should leave null out of your logic, then if some reference is null, you know that for sure something really nasty happen, not related to your model. Inside Location you can create for example a method isEmpty() or similar, so you can easily avoid comparing to null.

0
Dawood ibn Kareem On

Use an enum with the values BLACK, WHITE and VACANT, instead of a String, to store what colour token is at each location. Return the same enum from getColor() in the Player class.