Home > Software engineering >  Object inside matrix is returning null
Object inside matrix is returning null

Time:01-08

I'm having trouble understanding why I'm getting "Cannot read field "pieceColor" because "elem" is null", in this line "if(elem.pieceColor == pieceColor){"

Desired outcome: Given a color of a piece, for example black. Count every black piece in the board, and sum their value, according to the enum "PieceType".

I hope I was clear. Thank you!

import m2_praticas.xadrez.enums.PieceColor;

public class Main {
    public static void main(String[] args) {
        Board board = new Board();

        System.out.println(board.avalia(PieceColor.BLACK));
    }
}
import m2_praticas.xadrez.enums.PieceColor;
import m2_praticas.xadrez.enums.PieceType;

public class Board {
    Piece board[][];

    public Board(){
        board = new Piece[8][8]; // Creates a new board

        // For loop to set the pieces
        for (int i = 0; i < board.length; i  ){ // columns
            for (int j = 0; j < board[i].length; j  ){ // rows
                //System.out.println("["   i   "]"   "["   j   "]");
                if(i == 0 && j >= 0){ // 0, 0 & 0, 1
                    board[i][j] = new Piece(PieceColor.BLACK, PieceType.ROOK); // 2 black rooks
                }
                if(i == 0 && j >= 2){
                    board[i][j] = new Piece(PieceColor.BLACK, PieceType.KNIGHT); // 2 black knights
                }
                if(i == 0 && j >= 4){
                    board[i][j] = new Piece(PieceColor.BLACK, PieceType.BISHOP); // 2 black bishops
                }
                if(i == 0 && j >= 6){
                    board[i][j] = new Piece(PieceColor.BLACK, PieceType.QUEEN); // 1 black queen
                }
                if(i == 0 && j >= 7){
                    board[i][j] = new Piece(PieceColor.BLACK, PieceType.KING); // 1 black king
                }
                if(i == 1 && j >= 0){
                    board[i][j] = new Piece(PieceColor.BLACK, PieceType.PAWN); // 8 black pawns
                }
                // ---------------------------------
                if(i == 6 && j >= 0){
                    board[i][j] = new Piece(PieceColor.WHITE, PieceType.PAWN); // 8 white pawns
                }
                if(i == 7 && j >= 0){
                    board[i][j] = new Piece(PieceColor.WHITE, PieceType.ROOK); // 2 white rooks
                }
                if(i == 7 && j >= 2){
                    board[i][j] = new Piece(PieceColor.WHITE, PieceType.KNIGHT); // 2 white knights
                }
                if(i == 7 && j >= 4){
                    board[i][j] = new Piece(PieceColor.WHITE, PieceType.BISHOP); // 2 white bishops
                }
                if(i == 7 && j >= 6){
                    board[i][j] = new Piece(PieceColor.WHITE, PieceType.QUEEN); // 1 white queen
                }
                if(i == 7 && j >= 7){
                    board[i][j] = new Piece(PieceColor.WHITE, PieceType.KING); // 1 white king
                }
            }
        }
    }

    public int avalia(PieceColor pieceColor){
        int totalValue = 0;

        for (Piece[] p: board){
            for (Piece elem: p){
                if(elem.pieceColor == pieceColor){
                    switch (elem.pieceType){
                        case PAWN:
                            totalValue = (totalValue   PieceType.PAWN.getPieceValue());
                        break;
                        case ROOK:
                            totalValue = (totalValue   PieceType.ROOK.getPieceValue());
                        break;
                        case KNIGHT:
                            totalValue = (totalValue   PieceType.KNIGHT.getPieceValue());
                        break;
                        case BISHOP:
                            totalValue = (totalValue   PieceType.BISHOP.getPieceValue());
                        break;
                        case QUEEN:
                            totalValue = (totalValue   PieceType.QUEEN.getPieceValue());
                        break;
                        case KING:
                            totalValue = (totalValue   PieceType.KING.getPieceValue());
                        break;

                    }
                }
            }
        }
        return totalValue;
    }
}
import m2_praticas.xadrez.enums.PieceColor;
import m2_praticas.xadrez.enums.PieceType;

public class Piece {
    PieceColor pieceColor;
    PieceType pieceType;

    public Piece(PieceColor pieceColor, PieceType pieceType) {
        this.pieceColor = pieceColor;
        this.pieceType = pieceType;
    }
}
public enum PieceType {
    PAWN(1), ROOK(5), KNIGHT(3), BISHOP(3), QUEEN(9), KING(0);

    private int pieceValue;

    PieceType(int pieceValue){
        this.pieceValue = pieceValue;
    }

    public int getPieceValue() {
        return pieceValue;
    }
}
public enum PieceColor {
    BLACK, WHITE;
}

CodePudding user response:

Items in Java arrays are allowed to be null. Your Board constructor adds pieces to the board but there are no pieces in rows 2, 3, 4 or 5. Those pieces are null.

You must check for null before checking pieceColor

for (Piece elem: p){
  if(elem != null && elem.pieceColor == pieceColor){

or

for (Piece elem: p){
  if (elem == null) continue;
  if(elem.pieceColor == pieceColor){

There are annotations like @CheckForNull that can be used with an IDE or tools like spotbugs to require checking for null. Or you can use the Optional class .

  • Related