I got a problem right here.World class
As i execute the code and reach 12 points everything works fine but I get this error:
java.lang.NullPointerException
at andreas.checkTouching(davis.java:31)
at andreas.act(davis.java:21)
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)
It probably has to do with me removing all objects and the code still trying to be executed . I tried to remove that by using the line but still no changes. I really need help here.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class andreas here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class andreas extends Actor
{
/**
* Act - do whatever the andreas wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(andreas.class != null && getWorld() != null)
{
setLocation(getX()-3,getY());
checkTouching();
}
}
public void checkTouching()
{
if(isTouching(explosion.class))
{
removeTouching(explosion.class);
MyWorld myWorld = (MyWorld)getWorld();
myWorld.addScore(2);
getWorld().addObject(new missile(), 35, 375);
Greenfoot.playSound("sounds/andreas.mp3");
getWorld().removeObject(this);
return;
}
if(isTouching(andreas.class))
{
getWorld().addObject(new andreas(),1550, Greenfoot.getRandomNumber(900));
getWorld().removeObject(this);
return;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.awt.Color;
import java.awt.Font;
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
public int img;
private int age=0;
public int score;
/**
* Constructor for objects of class MyWorld.
*
*/
GreenfootImage ge2 = new GreenfootImage("images/landschaft2.png");
GreenfootImage ge3 = new GreenfootImage("images/landschaft3.png");
GreenfootImage ge4 = new GreenfootImage("images/landschaft4.png");
GreenfootImage ge5 = new GreenfootImage("images/landschaft5.png");
GreenfootImage ge6 = new GreenfootImage("images/landschaft6.png");
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1600, 900, 1);
imgset();
prepare();
score = 0;
showScore();
}
public void imgset()
{
age++;
if (age<2)
{
img = (Greenfoot.getRandomNumber(5));
if (img == (0))
{
this.getBackground().drawImage(ge2,0,0);
}
if (img == (1))
{
this.getBackground().drawImage(ge3,0,0);
}
if (img == (2))
{
this.getBackground().drawImage(ge4,0,0);
}
if (img == (3))
{
this.getBackground().drawImage(ge5,0,0);
}
if (img == (4))
{
this.getBackground().drawImage(ge6,0,0);
}
age = age+1;
}
}
public void act()
{
if (Greenfoot.getRandomNumber(150) < 1)
{
addObject(new andreas(), 1550, Greenfoot.getRandomNumber(900));
}
if (Greenfoot.getRandomNumber(150) < 1)
{
addObject(new davis(), 1550, Greenfoot.getRandomNumber(900));
}
}
public void prepare()
{
missile Missile = new missile();
addObject(new missile(),35,375);
Abschussrampe abschussrampe = new Abschussrampe();
addObject(new Abschussrampe(),63,370);
}
private void showScore()
{
showText("Score: " + score, 100, 50);
}
public void addScore(int points)
{
score = score + points;
showScore();
if (score >= 12)
{
pointcheck10();
}
if (Greenfoot.isKeyDown("2"))
{
score = score + 20;
}
}
public void pointcheck10()
{
removeObjects(getObjects(null));
addObject(new TextField(200, 40, Color.black, Color.red ,"Level 1 complete"), 800, 450);
addObject(new TextField(200, 40, Color.magenta, Color.black ,"Explosiondamage"), 500, 800);
addObject(new TextField(200, 40, Color.red, Color.black ,"Faster Rocket"), 800, 800);
addObject(new TextField(200, 40, Color.blue, Color.black ,"Slower Targets"), 1100, 800);
Greenfoot.delay(2);
}
}
if(andreas.class != null && getWorld() != null)
