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) ?