I'm having an issue with this switch. The collision method is exactly the same as canSee from animal class in all the examples. The spawn method just adds an object with some randomness, and when I call just spawn from the act method there are no issues, but from the switch it adds multiple (anywhere from 5 to 30) asteroids and then throws an error, neither of which it should do. the error is
java.lang.NullPointerException
at Asteroid.spawn(Asteroid.java:111)
at Asteroid.collideAsteroid(Asteroid.java:79)
at Asteroid.act(Asteroid.java:25)
and always points to one of the spawn calls in the switch. Maybe I'm missing something obvious but any help would be appreciated. If you need any other code or have questions let me know. Thanks!
public void collideAsteroid() { if(collision(Asteroid.class) && blownUp==false) { getWorld().removeObjects(getObjectsAtOffset(0,0,null)); //setImage("Explosion1.png"); blownUp=true; asteroidSpeed=0; } if (blownUp=true) { whereToSpawn=(Greenfoot.getRandomNumber(10)); switch(whereToSpawn) { case 0: spawn(0,0); break; case 1: spawn(0,80); break; case 2: spawn(0,160); break; case 3: spawn(0,240); break; case 4: spawn(0,320); break; case 5: spawn(0,400); break; case 6: spawn(0,480); break; case 7: spawn(0,560); break; case 8: spawn(0,640); break; case 9: spawn(0,720); break; default: spawn(0,0); break; } blownUp=false; }