Hi!
I was wondering if what is in the title right now is indeed possible, since it throws a NullPointerException.
The situation is: I have troops that are sent onto Resource.class objects. When the troop reaches the resource, it starts damaging it every few acts, by calling a method of the Resource.class object itself. When the health reaches 0 or less, the resource calls a "getWorld().removeObject(this);". The NullPointerException is called at that point. BUT: the resource has been deleted from the world (no longer visible).
The problem appeared when I tried to make the troop look for another Resource.class object.
Here's my code, in order of what calls what:
act() method of Troop1.class (subclass of Troop.class):
the attack() method of Troop.class:
the removeHealth() method of Resource.class:
finally, the getNearestResource() method of Troop.class (it is supposed to find the next targetResource of the troop):
thank you very much for your help! :D
EDIT 1: the exact exception that is thrown is:
java.lang.NullPointerException
at Resource.removeHealth(Resource.java:73)
at Troop.attack(Troop.java:58)
at Troop1.act(Troop1.java:33)
at greenfoot.core.Simulation.actActor(Simulation.java:604)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
at greenfoot.core.Simulation.runContent(Simulation.java:221)
at greenfoot.core.Simulation.run(Simulation.java:211)
java.lang.NullPointerException
at Resource.removeHealth(Resource.java:73)
at Troop.attack(Troop.java:58)
at Troop1.act(Troop1.java:33)
at greenfoot.core.Simulation.actActor(Simulation.java:604)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
at greenfoot.core.Simulation.runContent(Simulation.java:221)
at greenfoot.core.Simulation.run(Simulation.java:211)
public void act()
{
// check for resources on its path to go to
Resource resource = getOneResource(perceptionRadius);
if(resource != null && !hasChosenTarget){
hasChosenTarget = true;
this.currentTarget = resource;
// set new destination to center of touched resource, with a slight offset (method in Troop.class)
setNewOffsetDestination(currentTarget.getX(), currentTarget.getY());
}
// check if the troop needs to attack its currentTarget
if(hasReachedCurrentTarget()){
this.isAttacking = true;
}
if(isAttacking){
attack();
attackDelay++;
}
// move
moveToDestination(destX, destY);protected void attack(){
if(currentTarget == null){
isAttacking = false;
this.currentTarget = getNearestResource();
setNewOffsetDestination(currentTarget.getX(), currentTarget.getY());
}
if(isAttacking == true && attackDelay >= reloadTime){
currentTarget.removeHealth(attackDamage);
attackDelay = 0;
}
}public void removeHealth(int damageReceived){
this.health -= damageReceived;
if(health <= 0){
getWorld().removeObject(this);
}
}protected Resource getNearestResource(){
Resource resource = null;
for (int i = 0; resource == null; i++){
List<Resource> touchedResources = getNeighbours(i, true, Resource.class);
if(!touchedResources.isEmpty()){
resource = touchedResources.get(0);
}
}
return resource;
}


