-2

I am implementing a chess engine so i've constructed the following function to test possible moves(without actually making one) for the AI part of the engine.

The function takes as parameters the side(black or white),an object of type board that contains an arraylist of all the pieces of the chessboard as well as some other information and the source and destination squares of the move as strings.

My problem is that the moves aren't "made" properly.What i mean is that in the board object that is returned after this function is finished the pieces have no changed row or col even though if the function changes them.The board only changes if a piece is removed from it.

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;
}

As you can see the object piece is defined as following:

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

I have concluded that i must change it to:

Piece piece =b.notationToPiece(srcSq);

After thinking that the only thing that happens correctly is the removal of a piece.Thus changing something directly to the board that is to be returned works.

So i decided that the changes the function makes must affect the piece that is part of the arraylist of the object b and not some new object.

However when i try to do this i get the error:

java.lang.NullPointerException

At the line:if (piece.getColor()==side) {

That is quite strange since this function for testing moves is almost identical to the function i use for making moves(that works fine) which is the following:

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
    }

I can't find the source of the problem.

Community
  • 1
  • 1
  • `"I can't find the source of the problem."` -- at this point, I fear that only you **can** find the source, since only you have runnable code. Use a debugger and find the null variable that causes the NPE to be thrown, and then trace back to see why. – Hovercraft Full Of Eels Mar 11 '16 at 22:29

1 Answers1

0

Your notationToPiece() method is returning a null Piece object based on the srcSq String that has been entered to that method. Your error lies in there, I suspect.

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

Community
  • 1
  • 1
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • First of all thank you for the answer.Yes you make a good point however in the second function i have posted, that works fine, i use the same method without any problem. – Agisilaos Vasileiadis Mar 11 '16 at 22:28
  • Check whether your Piece that is returned from the function is null. If it is, then you'll need to go back to the function that returns the Piece. If it's not that, then it's going to be the getColor() method, if it returns an Object. My assumption is that the Color is not an object, but a value type. – ManoDestra Mar 11 '16 at 22:29
  • It is null but in the working function the method never returns null if the coordinates are valid.And how does new Piece() not return null and makes the piece have all the information such as color,row and col? – Agisilaos Vasileiadis Mar 11 '16 at 22:37
  • If this line `if (piece.getColor()==side) {` is causing the issue, then your piece is null and you need to trace back to where it could be null. Either your notationToPiece is returning a null Piece object, or your piece variable is being set elsewhere. Check your piece object for null `if (piece != null) {` and do some console output and work backwards to find the cause. You haven't posted your notationToPiece method's code, so I can't advise on that directly. – ManoDestra Mar 11 '16 at 22:48