0

I can't seem to use an ArrayList, or LinkedList, for some reason. I am able to use them in the method below the 'calculateLegalMoves' method but for some reason only that method seems to have a problem.

Error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Collection.toArray()" because "c" is null
    at java.base/java.util.ArrayList.addAll(ArrayList.java:670)
    at board.Board.calculateLegalMoves(Board.java:52)
    at board.Board.<init>(Board.java:38)
    at board.Builder.build(Builder.java:28)
    at board.Board.createStandardBoard(Board.java:143)
    at gui.BoardGUI.<init>(BoardGUI.java:29)
    at main.Main.main(Main.java:12)

Code:

package board;

import com.google.common.collect.ImmutableList;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import board.tile.Tile;
import board.tile.TileData;
import piece.Piece;
import piece.Team;
import piece.type.Bishop;
import piece.type.King;
import piece.type.Knight;
import piece.type.Pawn;
import piece.type.Queen;
import piece.type.Rook;
import player.BlackPlayer;
import player.WhitePlayer;


public class Board {
    static List<Tile> gameBoard;
    Collection<Piece> whitePieces;
    Collection<Piece> blackPieces;
    
    private WhitePlayer whitePlayer;
    private BlackPlayer blackPlayer;
    
    public Board (Builder builder) {
        Board.gameBoard = createGameBoard(builder);
        tileDistance();
        
        this.whitePieces = calculateActivePieces(this.gameBoard, Team.WHITE);
        this.blackPieces = calculateActivePieces(this.gameBoard, Team.BLACK);
        Collection<Move> whiteStandardLegalMoves = calculateLegalMoves(this.whitePieces);
        Collection<Move> blackStandardLegalMoves = calculateLegalMoves(this.blackPieces);
        
        this.whitePlayer = new WhitePlayer(this, whiteStandardLegalMoves, blackStandardLegalMoves);
        this.blackPlayer = new BlackPlayer(this, whiteStandardLegalMoves, blackStandardLegalMoves);
    }
    
    
    
    private Collection<Move> calculateLegalMoves(Collection<Piece> pieces) {

        List<Move> legalMoves = new ArrayList<>();
        
        for (Piece piece : pieces) {
            legalMoves.addAll(piece.generateMoves(this));
        }
        
        return ImmutableList.copyOf(legalMoves);
    }



    private Collection<Piece> calculateActivePieces(List<Tile> gameBoard, Team team) {
        
        List<Piece> activePieces = new ArrayList<>();
        
        for (Tile tile : gameBoard) {
            if (tile.getTilePiece() != null) {
                Piece piece = tile.getTilePiece();
                if (piece.getPieceTeam() == team) {
                    activePieces.add(piece);
                }
            }
        }
        
        return activePieces;
    }
}

I have tried so much, but since I am newish to java and programming, and the fact that I don't even understand the issue at hand since searching up the error yields next to no valuable results, I'm stumped on what to do. It might even be a really simple fix but I'm just so confused

I can also the code in one of the child classes of the Piece class if that is needed to find the error.

bluJay
  • 1
  • It might, I just don't know which type of NPE my error is – bluJay Mar 25 '23 at 12:35
  • 2
    There's just one type of NPE, and the stack trace shows you where it occurs: `at board.Board.calculateLegalMoves(Board.java:52)`. So `piece.generateMoves(this)` returns `null`, but we cannot tell because this part of the code is missing. – QBrute Mar 25 '23 at 12:45

0 Answers0