#NullPointerException : Cannot assign field "x" because "this.activeP" is null

1 messages · Page 1 of 1 (latest)

near flicker
#

Hello, so I was following a youtube chess game tutorial and I got stuck with this problem (on the first screenshot). I did everything on the video (https://www.youtube.com/watch?v=jzCxywhTAUI&t=1783s) yet I cant solve the issue, I tried giving activeP parameters by creating a new piece object like " Piece activeP = new Piece(WHITE,1,2);". The error disappears by doing so but I still can't move the chess piece. It feels like the issue is easy to solve but I just can't find the solution. I am not very good with Java so if anyone can help me with it I would very happy

I coded chess in Java and this is how I did it.

It looked simple enough but boy, it was much more complicated than I thought! I was surprised at how much you can learn by coding this little old game.

You can download the chess piece images in the video from here:
https://ryisnow.itch.io/pixel-art-chess-piece-images

About Game Loop:
https://...

▶ Play video
final torrentBOT
#

<@&987246399047479336> please have a look, thanks.

steady cairn
#

try to debug your code
add breakpoint at the line before the exception happened then from your IDE menu click debug
then keep move on step by step till you get the cause of the issue

#

from the error msg it say that at line 133 you tried to get access x field of activep which currently is null

near flicker
steady cairn
#

i think the else part that call simulate should be inside the if(mouse.pressed)
i.e it should be else for the if(active==null)

#

@near flicker it just a guess as i do not know the full code and how things run

near flicker
near flicker
steady cairn
#

share the code in the image so i can modify it and resend it to you

#

and it's ok do not be sorry

#

@near flicker check 29:14 of the video and you will see the issue that i mentioned

near flicker
#
    public void update(){
        //Mouse button pressed
        if(mouse.pressed){
            if(activeP == null){
                //If the activeP is null, check if you can pick up a piece
                for(Piece piece : simPieces){
                    //If the mouse is on an ally piece, pick it up as the activeP
                    if(piece.color == currentColor && piece.col == mouse.x/Board.SQUARE_SIZE &&
                            piece.row == mouse.y/Board.SQUARE_SIZE){
                        activeP = piece;
                    }
                }
            }
        }
        else{
            //If the player is holding a piece, simulate the move
            simulate();
        }
    }
final torrentBOT
near flicker
#

@steady cairn Thank you so much, i understood what you tried to say

near flicker
#

I put the else statement at the end of the wrong if statement, very epic mistake

#

Really thank you a lot!

steady cairn
#

@near flicker you very welcome and it's ok we all fail for those kind of issue
that why debug and sometime different eye would help
and happy coding