This site requires JavaScript, please enable it in your browser!
Greenfoot back
RoverKnight
RoverKnight wrote ...

2021/6/26

List sometimes returns NullPointerException

RoverKnight RoverKnight

2021/6/26

#
So I'm currently working on programming chess and have 2 actors called "possibleMove" and "capturePossibility", which show what options a piece has concerning its movement. I have written the following methods (in the parent class of the 2 actors) to clear the board of these actors:
public void clearPosMoves(){
        List<possibleMove> possibleMoves = getWorld().getObjects(possibleMove.class);
        if(possibleMoves.isEmpty() == false) getWorld().removeObjects(possibleMoves);
    }
    
    public void clearCapPos(){
        List<capturePossibility> capturePossibilities = getWorld().getObjects(capturePossibility.class);
        getWorld().removeObjects(capturePossibilities);
    }
When I call these methods within those 2 actors themselves, they work fine, even when none of those actors are currently on the board. However, when I try to call these methods from my pieces class, so as to remove those actors when a different chess piece is clicked, the first lines of both methods (where a List is declared) return a NullPointerException. The methods are called from the pieces class as follows:
squares s = new squares();
public void generatePossibleMoves(int x, int y){
        //there's code here that is unrelated to the problem
        s.clearCapPos();
        s.clearPosMoves();
        //there's code here that is unrelated to the problem
}
Note that a squares (parent class of the 2 actors) object is created and used to call these methods in order to circumvent the problem of the methods being non-static, so they couldn't be called otherwise since Greenfoot says that there they're being called from a static context.
RcCookie RcCookie

2021/6/26

#
The only way I can see a NullPointerException to be thrown in these lines is if the given actor is not in a world, and thus, getWorld() returns null. The main problem is that your methods are not static, they require an actor from that type to exist, AND it even has to be in that world. I think a better solution would be to move both methods into the world class directly and make them static. This also allowed you to leave the getWorld() away completely.
RcCookie RcCookie

2021/6/26

#
One more note: Just a convention, but you usually start class names with an uppercase letter. That clearly separates them from variables and makes the code more readable.
danpost danpost

2021/6/27

#
Unless the parent class extends to more than just those two classes, you should just be able to do this:
getWorld().removeObjects(getWorld().getObjects(squares.class));
without creating a new squares object. Otherwise, use the same format twice, using the class names possibleMove and capturePossibility.
RoverKnight RoverKnight

2021/6/28

#
@RcCookie I tried moving the generatePossibleMoves() and both clear methods to MyWorld, but it doesn't work as a static because they all call non-static methods within them like getObjects(), removeObjects(), getObjectsAt(), etc.
RoverKnight RoverKnight

2021/6/28

#
also I don't understand why actors cant find methods or variables in world if they're not static
danpost danpost

2021/6/28

#
RoverKnight wrote...
also I don't understand why actors cant find methods or variables in world if they're not static
But they can:
((MyWorld)getWorld()).generatePossibleMoves();
RoverKnight RoverKnight

2021/6/28

#
UPDATE: I fixed the original problem by moving the clearPosMoves() and clearCapPos() methods to the pieces class. Not quite sure why exactly that fixes it, since there's no pieces object in the world either (only it's subclasses, just like squares), but it's fixed! Thanks for the advice either way!
danpost danpost

2021/6/28

#
RoverKnight wrote...
there's no pieces object in the world either (only it's subclasses, just like squares),
Just to be clear, instances of a subclass are also considered instances of the classes they extend from. For example, possibleMove and capturePossibility instances ARE squares instances. My one liner above will remove all instances of both classes (as well as all instances of any other subclass of squares). So, if you have pieces subclass instances in the world, then you have pieces instances in the world.
RoverKnight RoverKnight

2021/6/29

#
Ah I see, thanks for the explanation =)
You need to login to post a reply.