Whenever I shoot my ships I want them to respawn, so I added the code form the Joy ofCode videos for respawning. It works until I shoot my alien ship, and it says NullPointerException, and shows the error on the last line of code(add object, and stuff) WHAT DO I DO???
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class AlienDroneShip here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class AlienDroneShip extends Enemy
{
/**
* Act - do whatever the AlienDroneShip wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveDownwards();
explodeIfPotatoHits();
}
public void moveDownwards()
{
int x = getX();
int y = getY();
int ny = getY()+1;
setLocation(x,ny) ;
}
public void explodeIfPotatoHits()
{ if(canSee(Potato.class))
{
getWorld().removeObject(this);
spawnNewShip();
}
}
public void spawnNewShip()
{
{
AlienDroneShip newAlienDroneShip;
newAlienDroneShip = new AlienDroneShip();
World world;
world = getWorld();
int worldWidth = 480;
int worldHeight = 800;
int x = Greenfoot.getRandomNumber(worldWidth);
int y = Greenfoot.getRandomNumber(worldHeight);
world.addObject(newAlienDroneShip, x-200, y);
}
}
}

