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

2014/7/6

Display Health Problem

SB001 SB001

2014/7/6

#
Hey Guys, I have a BIG problem. Say that I have a cat called cat1. cat1 gets hurt if it falls over an edge (I know, irony). cat1 has a health variable called
public int cat1Health;
. There is a class called catHealth display with the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Shows Cats Health
 * 
 * @author SB001 - Samuel.T.Brown
 * @version 1.0
 */
public class catHealthDisplay extends text {
    private int catsHealth = 1000;
    
    public catHealthDisplay() {
        setImage(new GreenfootImage(200, 30));
        getHealth();
        update();
    }
    
    public void update() {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(gold); //gold is declared in the text class
        img.drawString("Cats Health: " + catsHealth, 4, 20);
    }
    
    public void getHealth() {
        int catsHealth = ((cat1) getWorld().getObjects(cat1.class).get(0)).displayHealth();
    }
}
The text class extends Actor and declares some colors using java.awt.Color. The cat has a
public int displayHealth()
that returns cat1Health. When I run this code i get a java.lang.NullPointerException. What's the problem??? Thanks in advance, Samuel.T.Brown Note: This is hyperthetical. I am actually creating a game very different to this and I am reluctant to show others the code before I am complete as this is a big project. If it is absolutely neccicary though I will show you my actual code.
danpost danpost

2014/7/6

#
One problem is that the constructor method (lines 12 through 16) will only execute at the time you create the object of the class (before the object is placed into the world). When line 26 is executed (by way of the call on line 14), 'getWorld' will return 'null'; and you cannot 'getObjects' from a 'null' (non-existent) World object. HINT: you are missing an 'act' method.
SB001 SB001

2014/7/6

#
Okay I will try this. I'm going to a place where there is no internet for 4 days so I will get back on whether this works then.
SB001 SB001

2014/7/7

#
Turns out there is WiFi... Could you give me an example of how this would work? I still can't figure it out. Thanks, Samuel.T.Brown
danpost danpost

2014/7/7

#
You just need to remove line 14 from the constructor and add an 'public void act' method that gets the health value and updates the image (calls 'getHealth' and 'update').
SB001 SB001

2014/7/7

#
I have but it just shows:
Cats Health: 1000
And Doesnt Change!
danpost danpost

2014/7/7

#
Please post code of cat1 class.
SB001 SB001

2014/7/9

#
SB001 wrote...
Note: This is hyperthetical. I am actually creating a game very different to this and I am reluctant to show others the code before I am complete as this is a big project. If it is absolutely neccicary though I will show you my actual code.
As I said before it is a hypothetical situation.
danpost danpost

2014/7/9

#
Remove 'int' from line 26. You want to set the already-existing 'catsHealth' field to the value acquired from the 'cat1' object, not create a new local variable that just goes to the garbage collector after being set.
You need to login to post a reply.