1
public boolean hasCapturableEnemy(Piece[][] board, int startX, int startY) {

    //If the Pawn belong to the Upper Team (Faces downward)
    if(board[startX][startY].getTeam() == Player.UP) {

        //If a Piece exists diagonally in front of it on an adjacent tile, return true
        if((board[startX - 1][startY + 1].getTeam() != Player.UP) || (board[startX - 1][startY - 1].getTeam() != Player.UP)) {
            return true;
        } else {
            return false;
        }

    } else { //If the Pawn belongs to the Down Team (Faces upward)

        //If a Piece exists diagonally in front of it on an adjacent tile, return true
        if((board[startX + 1][startY + 1].getTeam() != Player.DOWN) || (board[startX + 1][startY - 1].getTeam() != Player.DOWN)) {
            return true;
        } else {
            return false;
        }
    }
}

I built a boolean function that tries to check if a Pawn chess piece has an enemy to capture. On the condition of checking whether the piece on the diagonal side of the Pawn is an enemy or not, I get a NullPointerException since I am checking for both diagonal position and one of them could be an empty location (null).

(board[startX + 1][startY + 1].getTeam() != Player.DOWN) 
//This could be NULL

How can I avoid getting a NullPointerException while checking for both position and checking their member variable (Team) ?

user6792790
  • 668
  • 1
  • 7
  • 22
  • 1
    Have the board (or a helper class) return a list of (non-null) positions and check only those. – Fildor Sep 15 '17 at 07:26
  • 1
    you need to check that ***board[startX - 1][startY + 1] is != null*** – ΦXocę 웃 Пepeúpa ツ Sep 15 '17 at 07:26
  • 1
    @ΦXocę웃Пepeúpaツ If I check board[startX - 1][startY + 1] is != null || board[startX - 1][startY - 1] is != null, one of them could still be null and would still give me a NullPointerException. Wouldn't it? – user6792790 Sep 15 '17 at 07:27
  • `board[startX + 1][startY + 1] != null && board[startX + 1][startY + 1].getTeam() != ...`, repeated 5 times is too long. I'd say write a `getTeamAt(Place[][] board, int x, int y)` that returns `null` if `board[x][y]` is `null`, and `board[x][y].getTeam()` otherwise. – johnchen902 Sep 15 '17 at 07:30
  • 1
    "If I check board[startX - 1][startY + 1] is != null || board[startX - 1][startY - 1] is != null, one of them could still be null and would still give me a NullPointerException." - You should examine each position separately. `a!=null && a.SomeBoolMethod()` whill not throw, because in case of a==null the first part makes the whole expression false and the second part won't be evaluated. So you can do _this_ in one "if". – Fildor Sep 15 '17 at 07:40
  • 1
    @Jens According to what OP wrote I think he knows what an NPE is. And the most straightforward solution (add a null-check for every use) is too long and unmaintainable here. – johnchen902 Sep 15 '17 at 07:41
  • To add to my previous comment: But you should rather do something like a Utility, that will return possible Positions of the board depending on coords and Piece, taking board boundaries into account. So you "outsource" those concerns and can concentrate on the logic. – Fildor Sep 15 '17 at 07:45

0 Answers0