Can someone please tell me what I need to change in my code? I got this error:
java.lang.NullPointerException
at Turtle.ifInLevel1(Turtle.java:171)
at Turtle.act(Turtle.java:20)
at greenfoot.core.Simulation.actActor(Simulation.java:565)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
at greenfoot.core.Simulation.runContent(Simulation.java:213)
at greenfoot.core.Simulation.run(Simulation.java:203)
Here is my class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Turtle here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Turtle extends Animal
{
private int timer = 100;
private int counter;
private boolean isBlasterType2;
private int debrisPickedUp;
public void act()
{
ifInIntro();
ifInMainMenu();
ifInLevel1();
}
public void ifInIntro()
{
World world = getWorld();
if (world instanceof Intro)
{
if(timer==0)
{
move(2);
if(getRotation() == 0 && getX()>=355) timer = 136;
if(getRotation() == 180 && getX()<=202) Greenfoot.setWorld(new MainMenu());
}
else
{
timer--;
if(getX()>=355 && timer<36) setRotation(getRotation()-5);
}
if (canSee(Turtle.class))
{
eat(Turtle.class);
}
}
}
public void ifInMainMenu()
{
World world;
world = getWorld();
if (world instanceof MainMenu)
{
counter++;
timer = 100;
if (counter == 1)
{
turnTowards(300, 0);
counter++;
}
if (Greenfoot.isKeyDown("up"))
{
move(3);
}
if (Greenfoot.isKeyDown("down"))
{
move(-3);
}
if (Greenfoot.isKeyDown("left"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("right"))
{
turn(5);
}
if (getX() >= 245 && getX() <= 365 && getY() <= 115)
{
Greenfoot.delay(75);
Greenfoot.setWorld(new Level1());
}
}
}
public void ifInLevel1()
{
World world = getWorld();
if (world instanceof Level1)
{
move(3);
if (Greenfoot.isKeyDown("left"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("right"))
{
turn(5);
}
if (Greenfoot.isKeyDown("x"))
{
turn(25);
shoot();
shoot();
shoot();
}
String key=Greenfoot.getKey();
if (key == null) return;
if ("z".equals(key) ) isBlasterType2= !isBlasterType2;
if ("space".equals(key)) shoot();
Actor arrow = getOneIntersectingObject(Arrow.class);
if (arrow != null )
{
world.removeObjects(world.getObjects(Rock.class));
world.removeObjects(world.getObjects(Text.class));
world.removeObjects(world.getObjects(Arrow.class));
setLocation(300, 490);
Debris debris = new Debris();
world.addObject(debris, 83, 449);
Debris debris2 = new Debris();
world.addObject(debris2, 246, 400);
Debris debris3 = new Debris();
world.addObject(debris3, 387, 342);
Debris debris4 = new Debris();
world.addObject(debris4, 532, 387);
Debris debris5 = new Debris();
world.addObject(debris5, 408, 195);
Debris debris6 = new Debris();
world.addObject(debris6, 408, 195);
Debris debris7 = new Debris();
world.addObject(debris7, 497, 99);
Debris debris8 = new Debris();
world.addObject(debris8, 300, 99);
Debris debris9 = new Debris();
world.addObject(debris9, 135, 93);
Debris debris10 = new Debris();
world.addObject(debris10, 114, 275);
}
if(canSee(Debris.class))
{
eat(Debris.class);
debrisPickedUp++;
}
if (debrisPickedUp == 10)
{
Arrow arrow1 = new Arrow();
world.addObject(arrow1,300,470);
arrow.setImage("ArrowDownB.png");
}
}
}
public void shoot()
{
Actor blaster = null;
if (isBlasterType2)
{
blaster = new Bullet2();
blaster.setImage("BBPurge.png");
}
else
{
blaster = new Bullet();
}
blaster.setRotation(getRotation());
getWorld().addObject(blaster, getX(), getY());
}
public void tryToEat()
{
if (canSee(Turtle.class))
{
eat(Turtle.class);
}
}
public void createNewBullet()
{
Bullet b;
b = new Bullet();
World world;
world = getWorld();
world.addObject(b,getX() ,getY() );
}
/**
* Method createNewCRock
*
*/
public void createNewCRock()
{
Universal cr;
cr = new Universal();
World world;
world = getWorld();
int x = getX();
int y = getY();
cr.setImage("CRock.png");
world.addObject(cr,475,441);
}
}
