无法解决nullPointerException

时间:2016-03-11 22:22:18

标签: java class nullpointerexception

我正在实施一个国际象棋引擎,所以我构建了以下函数来测试引擎的AI部分可能的移动(实际上没有制作)。

该函数将侧面(黑色或白色)作为参数,一个类型板的对象,其中包含棋盘的所有部分的arraylist以及一些其他信息以及作为字符串的移动的源和目标方块。

我的问题是移动没有正确“制作”。我的意思是在完成此功能后返回的棋盘对象中,即使函数更改它们,碎片也没有更改的行或列。如果从中取出一块,则该板仅会改变。

public Board doTestMove(int side,Board b, String srcSq, String destSq) {
        //check source square notation for validity

    if (checkSqValidity(srcSq)) {

        //check destination square notation for validity
        if (checkSqValidity(destSq)) {
            //find the piece based on the source square notation.

                            Piece piece =new Piece(b.notationToPiece(srcSq));
                            if(piece==null){
                                System.out.println("The piece is null");
                            }
            //make sure the piece is owned by the player
            if (piece.getColor()==side) {
                //get all movements that are allowed for the selected piece
                                    ArrayList<ArrayList<Integer>> legalMoves=new ArrayList<ArrayList<Integer>>();
                legalMoves = possiblePieceMoves(piece, false);
                //array coordinates for new destination
                Index newLoc = new Index(b.notationToIndex(destSq).getX(),b.notationToIndex(destSq).getY());
                //find out if destination location is included in the legal moves list
                ArrayList<Integer> x = legalMoves.get(0); //list of row numbers
                ArrayList<Integer> y = legalMoves.get(1); //list of column numbers
                ListIterator<Integer> xList = x.listIterator();  //row iterator
                ListIterator<Integer> yList = y.listIterator();  //column iterator
                int xL, yL;
                while (xList.hasNext() && yList.hasNext()) { //while lists have coordinates
                    //listiterator next() method doesn't work inside if statement -> assign to variables
                    xL = xList.next();
                    yL = yList.next();
                    if (newLoc.getX()==xL && newLoc.getY()==yL) { //legal move

                        b.removePiece(newLoc.getX(), newLoc.getY()); //remove captured piece from the board
                        piece.setRow(newLoc.getX()); //change piece row
                        piece.setCol(newLoc.getY()); //change piece column



                        b.updateGameState(); //populate the board with new location of pieces.
                        //place source and destination square to history of moves
                        if (side==0) { //if white
                            getHistoryOfMoves().addWhiteMove(srcSq, destSq); //add white piece move to history
                        } else if (side==1) { //if black
                            getHistoryOfMoves().addBlackMove(srcSq, destSq); //add black piece move to history
                        }
                        //promote pawns to queens if they reach enemy's end
                        b.promotePawnsToQueen(side);

                        return new Board(b); //move successful
                    }
                }
            }
        } else {
            System.out.println("Not a valid destination square. ");
        }
    } else {
        System.out.println("Not a valid source notation.");
    }
            return null;
}

如您所见,对象块定义如下:

Piece piece =new Piece(b.notationToPiece(srcSq));

我已经得出结论,我必须将其更改为:

Piece piece =b.notationToPiece(srcSq);

在认为唯一正确发生的事情就是删除一块。因此,将某些东西直接更改为要返回的棋盘会有效。

所以我决定函数所做的更改必须影响作为对象b的arraylist的一部分而不是某个新对象的部分。

然而,当我尝试这样做时,我得到错误:

  

java.lang.NullPointerException

     

行:if (piece.getColor()==side) {

这很奇怪,因为这个用于测试移动的函数几乎与我用于进行移动的函数(工作正常)完全相同,如下所示:

public boolean doMove(Player player, String srcSq, String destSq) {
        //check source square notation for validity
        if (checkSqValidity(srcSq)) {
            //check destination square notation for validity
            if (checkSqValidity(destSq)) {
                //find the piece based on the source square notation.
                Piece piece = getBoard().notationToPiece(srcSq);
                //make sure the piece is owned by the player
                if (piece.getColor()==player.getSide()) {
                    //get all movements that are allowed for the selected piece
                    ArrayList<ArrayList<Integer>> legalMoves = possiblePieceMoves(piece, false);
                    //array coordinates for new destination
                    Index newLoc = getBoard().notationToIndex(destSq);
                    //find out if destination location is included in the legal moves list
                    ArrayList<Integer> x = legalMoves.get(0); //list of row numbers
                    ArrayList<Integer> y = legalMoves.get(1); //list of column numbers
                    ListIterator<Integer> xList = x.listIterator();  //row iterator
                    ListIterator<Integer> yList = y.listIterator();  //column iterator
                    int xL, yL;
                    while (xList.hasNext() && yList.hasNext()) { //while lists have coordinates

                        xL = xList.next();
                        yL = yList.next();
                        if (newLoc.getX()==xL && newLoc.getY()==yL) { //legal move

                            getBoard().removePiece(newLoc.getX(), newLoc.getY()); //remove captured piece from the board
                            piece.setRow(newLoc.getX()); //change piece row
                            piece.setCol(newLoc.getY()); //change piece column




                            //place source and destination square to history of moves
                            if (player.getSide()==0) { //if white
                                getHistoryOfMoves().addWhiteMove(srcSq, destSq); //add white piece move to history
                            } else if (player.getSide()==1) { //if black
                                getHistoryOfMoves().addBlackMove(srcSq, destSq); //add black piece move to history
                            }
                            //promote pawns to queens if they reach enemy's end
                            getBoard().promotePawnsToQueen(player.getSide());
                            return true; //move successful
                        }
                    }
                }
            } else {
                System.out.println("Not a valid destination square. ");
            }
        } else {
            System.out.println("Not a valid source notation.");
        }
        return false; //move failed, not own piece
    }

我找不到问题的根源。

1 个答案:

答案 0 :(得分:0)

你的notationToPiece()方法根据输入到该方法的srcSq字符串返回一个null Piece对象。我怀疑你的错误就在那里。

...还 What is a NullPointerException, and how do I fix it?