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

2023/2/26

Why is this giving me a nullPointerExeption?

ReasonedTiger39 ReasonedTiger39

2023/2/26

#
 public void act()
    {
                setImage(new GreenfootImage(102, 52));
        getImage().drawRect(0,0,51,11);
        getImage().setColor(Color.RED);
        getImage().fillRect(1,1,health,10);
        World world = getWorld();
        if (getWorld() instanceof MyWorld){
            loseHealthMyWorld();
        }
        if (getWorld() instanceof HardRoom){
            loseHealthMyWorld();
        }
    }
    public void loseHealthMyWorld()
    {
        if(myWorld.getPlayer().hitByEnemy() == true)
        {
            health--;
        }
        else
        health = health;
        if (health<=0)
        {
            Greenfoot.stop();
        }
    }
    public void loseHealthHardRoom()
    {
        if(hardRoom.getPlayer().hitByEnemy())
        {
            health--;
        }
        if (health<=0)
        {
            Greenfoot.stop();
        }        
    }
This is the Player's method which is called:
        public boolean hitByEnemy()
    {
        Actor enemy = getOneObjectAtOffset(0,0, ENEMY.class);
        Actor slime = getOneObjectAtOffset(0,0, Slime.class);
        if(enemy!=null || slime!=null)
        {
            return true;
        }
        else
        return false;
    }
danpost danpost

2023/2/26

#
ReasonedTiger39 wrote...
Why is this giving me a nullPointerExeption? << Code Omitted >>
First, you will need to show the Error Trace (terminal output) -- copy/paste the full output here. Best if terminal is cleared and error is recreated before copying. From there, we will be able to narrow down where the error is coming from. My guess, however, is that either myWorld (line 17) or hardRoom (line 30), or both, do not have worlds assigned to them.
danpost danpost

2023/2/26

#
ReasonedTiger39 wrote...
<< Code Omitted >>
There is much that can be improved upon with your codes. (1) A class is a "definition" of an object; its members (fields and methods) describe the objects it creates. Fields give state and methods give behavior. The health field does not appear to be in the class of the player, but is a state of the player. The graphic user interface (GUI) object that shows the current health should not contain that field, (2) The image of the GUI object should not be updated every act step. Its image only needs to be changed when the value it represents changes. Also, GUI objects do not act on their own; so, no act methods should be in their classes. You may want to check out my Value Display Tutorial scenario. (3) You might want to consider using a common intermediate class between your game worlds and the World class. That way, you can put code common to both game worlds in the intermediate class and codes specific to each subclass in their respective classes. The ENEMY and Slime classes could also possibly use a common intermediate class before the Actor class. This, at minimum, would simplify the collision checking to the following (with intermediate class name of Foe)
if (getOneObjectAtOffset(0, 0, Foe.class != null)
to replace lines 3 thru 5 in the hitByEnemy method above. I am presuming that both ENEMY and Slime extend the Actor class directly.
ReasonedTiger39 ReasonedTiger39

2023/3/1

#
Thanks! I have solved the problem. Sorry for the late response.
You need to login to post a reply.