hey guys i have a lil problem with my code i try to make a healthbar
here is the code from the Player(engel):
here is the code from the healthbar:
at last here is the code from my World (Heaven):
and know here is the error by compilling the world wich is shown in the Terminal Window
java.lang.NullPointerException
at Engel.<init>(Engel.java:21)
at Heaven.populateWorld(Heaven.java:26)
at Heaven.<init>(Heaven.java:20)
pls help my thank you all guys :D
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Engel here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Engel extends Mover
{
/**
* Act - do whatever the Engel wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeys();
spawn();
}
int hp=((Healthbar)getWorld().getObjects(Healthbar.class).get(0)).hp--;
private boolean move = true;
private boolean spawned = true;
int teufel=2;
private void checkKeys()
{
String key = Greenfoot.getKey();
if ("9".equals(key) && move){ moveUpperRight(); move = false; spawned = true;}
if ("8".equals(key) && move){ moveUp(); move =false; spawned = true;}
if ("7".equals(key) && move){ moveUpperLeft(); move =false; spawned = true;}
if ("6".equals(key) && move){ moveRight();move =false; spawned = true;}
if ("4".equals(key) && move){ moveLeft();move =false; spawned = true;}
if ("3".equals(key) && move){ moveUnderRight();move =false; spawned = true;}
if ("2".equals(key) && move){ moveDown();move =false; spawned = true;}
if ("1".equals(key) && move){ moveUnderLeft();move =false; spawned = true;}
}
private void spawn()
{
if (teufel==30)
{
spawned= false;
}
if (!move)
{
if(Greenfoot.getRandomNumber(3)==0 && spawned)
{
getWorld().addObject(new Teufel(),Greenfoot.getRandomNumber(91),Greenfoot.getRandomNumber(71));
teufel++;
}
move = true;
spawned = false;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Healthbar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Healthbar extends Actor
{
/**
* Act - do whatever the Healthbar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Leben();// Add your action code here.
}
public int hp=100;
public int getHP()
{
return hp;
}
public void Leben()
{
if(hp>=150)
{
setImage("Lebensanzeige15.png");
}
if(hp>=140 && hp<150)
{
setImage("Lebensanzeige14.png");
}
if(hp>=130 && hp<140)
{
setImage("Lebensanzeige13.png");
}
if(hp>=120 && hp<130)
{
setImage("Lebensanzeige12.png");
}
if(hp>=110 && hp<120)
{
setImage("Lebensanzeige11.png");
}
if(hp>=100 && hp<110)
{
setImage("Lebensanzeige10.png");
}
if(hp>=90 && hp<100)
{
setImage("Lebensanzeige9.png");
}
if(hp>=80 && hp<90)
{
setImage("Lebensanzeige8.png");
}
if(hp>=70 && hp<80)
{
setImage("Lebensanzeige7.png");
}
if(hp>=60 && hp<70)
{
setImage("Lebensanzeige6.png");
}
if(hp>=50 && hp<60)
{
setImage("Lebensanzeige5.png");
}
if(hp>=40 && hp<50)
{
setImage("Lebensanzeige4.png");
}
if(hp>=30 && hp<40)
{
setImage("Lebensanzeige3.png");
}
if(hp>=20 && hp<30)
{
setImage("Lebensanzeige2.png");
}
if(hp>0 && hp<20)
{
setImage("Lebensanzeige1.png");
}
if(hp==0)
{
setImage("Lebensanzeige.png");
Greenfoot.stop();
// Greenfoot.playSound(" ");
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Heaven here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Heaven extends World
{
/**
* Constructor for objects of class Heaven.
*
*/
public Heaven()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(80, 60, 10);
populateWorld();
}
private void populateWorld()
{
addObject(new Teufel(),Greenfoot.getRandomNumber(80), Greenfoot.getRandomNumber(60));
addObject(new Teufel(),Greenfoot.getRandomNumber(80), Greenfoot.getRandomNumber(60));
addObject(new Engel(),40,30);
addObject(new Healthbar(),8,1);
addObject(new Telepilz(),Greenfoot.getRandomNumber(80), Greenfoot.getRandomNumber(60));
}
}

