I am building up on @AasmundEldhuset answer: You should check if != null and return false immediately. After the loop, just return true as all went well. You could also do away with the first if statement checking if differenceInRows is 1 as you would not enter the loop anymore for a difference of one.
Corrected code:
public boolean checkifEmpty(int fromRow, int fromColumn, int toRow,
int toColumn, Figure[][] ChessBoard) {
int differenceInRows = Math.abs(fromRow - toRow);
for (int j = 1; j < differenceInRows; j++) {
if ((toRow < fromRow) && (toColumn > fromColumn)
&& ChessBoard[fromRow - j][fromColumn + j] != null) {
return false;
} else if ((toRow > fromRow) && (toColumn > fromColumn)
&& ChessBoard[fromRow + j][fromColumn + j] != null) {
return false;
} else if ((toRow > fromRow) && (toColumn < fromColumn)
&& ChessBoard[fromRow + j][fromColumn - j] != null) {
return false;
} else if ((toRow < fromRow) && (toColumn < fromColumn)
&& ChessBoard[fromRow - j][fromColumn - j] != null) {
return false;
}
}
return true;
}
A final note to teach you how to fish instead of giving you fish: You should use the debugger within your IDE and step through the code until you find the bug. You should ideally create unit tests using JUnit or equivallent.
Happy programming!