when i try to run this it gives me a NullPointerException
import greenfoot.*;
/**
* Write a description of class Unit here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Unit extends Actor{
boolean Selected = false;
public byte lives = 6;
/**
*
* Act - do whatever the Unit wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act() {
healthBar();
}
public void healthBar(){
int x = getX();
int y = getY() - 35;
getWorld().addObject( new HealthBar() , x , y);
}
}
public class HealthBar extends Actor{
public byte lives = ((Unit)getWorld().getObjects(Unit.class).get(0)).lives;;
private byte healthBarWidth = 60;
private byte healthBarHight = 6;
private byte LengthPerHealth = (byte)(healthBarWidth/lives);
public void act(){
update();
}
/**
* This is were we create the method of selecting a unit
*/
public void update(){
setImage(new GreenfootImage(healthBarWidth + 2, healthBarHight + 2));
GreenfootImage healthImage = getImage();
healthImage.setColor(Color.WHITE);
healthImage.drawRect(0, 0, healthBarWidth + 1, healthBarHight + 1);
healthImage.setColor(Color.BLUE);
healthImage.fillRect(1, 1, healthBarWidth + 1, healthBarHight + 1);
}
public void hit(){
lives --;
}
}
