This site requires JavaScript, please enable it in your browser!
Greenfoot back
Yoshi21137
Yoshi21137 wrote ...

2020/3/5

java.lang.NullPointerException problems

Yoshi21137 Yoshi21137

2020/3/5

#
Anyone know how to fix this?
Yoshi21137 Yoshi21137

2020/3/5

#
private BossHealth bossHealth;

if (getWorld() != null)
        {
            bossHealth.setLocation(getX(), getY());
        }
danpost danpost

2020/3/5

#
Yoshi21137 wrote...
Anyone know how to fix this? << Code Omitted >>
Need a broader view of the class. Looks right now like lines 3 thru 6 are not located within a code block (where they should be).
Yoshi21137 Yoshi21137

2020/3/5

#
private BossHealth bossHealth;
int lives = 50;

public void act() 
    {
        Main main = (Main)getWorld();
        die();
        if (getWorld() != null)
        {
            bossHealth.setLocation(getX(), getY());
        }
    }
danpost danpost

2020/3/5

#
Yoshi21137 wrote...
<< Code Omitted >>
Looks to me like you have yet to assign your BossHealth object to the bossHealth field.
Yoshi21137 Yoshi21137

2020/3/5

#
I tried using this and it worked!
    public void act() 
    {
        if (getWorld() != null)
        {
            int x=getX();
            int y=getY()-90;
            Actor health = (Actor)getWorld().getObjects(BossHealth.class).get(0);
            health.setLocation(x,y);
        }
    }
danpost danpost

2020/3/5

#
Then, you can remove line 1 in your previous posts. The first line in act can be removed, as well. Also, when it dies, it should remove the health bar from the world, too (first).
You need to login to post a reply.