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:
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:
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.
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); }
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 }