Hello,
I am currently working on a space invaders project and no matter what I try I get the same error: java.lang.NullPointerException in terminal when one alien gets removed. I try to get a numAliens so that the game will be over if getObjects(Alien.class).size() == 0.
this is how they are spawned in the world
this is how I tried to get the amount of alien objects left in the world class
and this is how I try to use it in the Alien class
I also tried other if conditions like <2 etc.
I think the problem is the object remover
it should only remove one specific object and not the whole class
//"spawn" rows and columns of Aliens
for(int x=110;x<=390;x+=40)
{
for(int y=80;y<=200;y+=40)
{
addObject(new Alien(),x,y);
}
}public int getNumAliens()
{
/*List <Alien> aliens = getObjects(Alien.class);
numAliens = aliens.size();
return numAliens;
*/
numAliens = numberOfObjects();
return numAliens;
}/**
* Check if all Aliens are removed/ "dead"
*/
public void checkForWin()
{
Space space=(Space)getWorld();
if(space.getNumAliens() == 0)
{
space.showWin();
}
}/**
* Check if alien and shot interesect and remove both when true
*/
public void checkForHit(Class intersectingClass)
{
Actor actor = getOneIntersectingObject(intersectingClass);
if(actor!=null)
{
Space space = (Space)this.getWorld();
space.getCounter().add(getPoints());
space.removeObject(getOneIntersectingObject(Shot.class));
space.removeObject(this);
explosionSound.setVolume(70);
explosionSound.play();
}
}
