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

2020/4/30

NullPointerError

Evstar14 Evstar14

2020/4/30

#
I am new to coding, and I have been trying to change the image for my heart icons when the player lands on a spike. My only problem was accessing a variable from another actor, and I followed this tutorial for it: https://www.greenfoot.org/doc/howto-1 (Even though I don't fully understand how everything in it works). I am now getting a NullPointerException Error at line 18 of the player, which is here:
public void SpikeDeath()
    {
        if (OnSpike())
        {   
            HitCounter = HitCounter + 1;
            setLocation (74, 401);
            LoseHealth();
        }
        else if (HitCounter == 3)
        {
            Greenfoot.setWorld(new YouLose());
        }
    }
    public void LoseHealth()
    {
        Level1 Level1World = (Level1) getWorld();
        Health health = Level1World.getHealth();
        health.TakeDamage(1);
    }
That line links to the health code here:
public void TakeDamage(int amount)
    {
        TotalHealth += amount;
        if (TotalHealth == 1)
        {
            setImage (image2);
        }
        else if (TotalHealth == 2)
        {
            setImage (image3);
        }
        else if (TotalHealth == 3)
        {
            setImage (image4);
        }
    }
What is the problem, and how can i fix it? Thanks
danpost danpost

2020/4/30

#
Please show entire player class codes.
Evstar14 Evstar14

2020/4/30

#
The Error referenced lines 28, 210 and 221
import greenfoot.*;
public class Player extends Actor
{
    private GifImage gif1;
    private GifImage gif2;
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private int RunSpeed = 3;
    private int FallSpeed = 0;
    private int FallAcceleration = 1;
    private int CanJump = 0;
    private int CanWalkR = 0;
    private int CanWalkL = 0;
    private int HitCounter = 0;
    public Player()
    {
        gif1 = new GifImage("Player Running GIF R.gif");
        gif2 = new GifImage("Player Running GIF L.gif");
        image1 = new GreenfootImage("Player Still.png");
        image2 = new GreenfootImage("Player Jump R.png");
        image3 = new GreenfootImage("Player Jump L.png");
        GreenfootImage image = getImage();
        image.scale(22,55);
    }
    public void act() 
    {
        SpikeDeath();
        KeyCheck();
        NeedGravity();
        JumpReset();
        HitRoof();
        MoveFromGround();
    }   
    public void KeyCheck()
    {
        if (Greenfoot.isKeyDown("space") && !AtRoof() && CanJump == 0)
        {
            Jump();
            CanJump = 1;
        }
        else if (Greenfoot.isKeyDown("D") && !OnGround() && !ObjectRB() && !ObjectRT())
        {
            setImage(image2);
            GreenfootImage image = getImage();
            image.scale(50,67);
            MoveRight();
        }
        else if (Greenfoot.isKeyDown("A") && !OnGround() && !ObjectLB() && !ObjectLT())
        {
            setImage(image3);
            GreenfootImage image = getImage();
            image.scale(50,67);
            MoveLeft();
        }
        else if (Greenfoot.isKeyDown("D") && OnGround() && !ObjectRB() && !ObjectRT())
        {
            setImage(gif1.getCurrentImage());
            GreenfootImage image = getImage();
            image.scale(50,67);
            MoveRight();
        }
        else if (Greenfoot.isKeyDown("A") && OnGround() && !ObjectLB() && !ObjectLT())
        {
            setImage(gif2.getCurrentImage());
            GreenfootImage image = getImage();
            image.scale(50,67);
            MoveLeft();
        }
        else
        {
            setImage(image1);
            GreenfootImage image = getImage();
            image.scale(22,55);
        }
    }
    public void JumpReset()
    {
        if (CanJump == 1 && OnGround())
        {
            CanJump++;
        }
        else if (CanJump == 2 && OnGround())
        {
            CanJump++;
        }
        else if (CanJump == 3 && OnGround())
        {
            CanJump++;
        }
        else if (CanJump == 4 && OnGround())
        {
            CanJump++;
        }
        else if (CanJump == 5 && OnGround())
        {
            CanJump++;
        }
        else if (CanJump == 6 && OnGround())
        {
            CanJump++;
        }
        else if (CanJump == 7 && OnGround())
        {
            CanJump++;
        }
        else if (CanJump == 8 && OnGround())
        {
            CanJump++;
        }
        else if (CanJump == 9 && OnGround())
        {
            CanJump = 0;
        }
    }
    public void MoveRight()
    {
        setLocation (getX() + RunSpeed, getY());
    }
    public void MoveLeft()
    {
        setLocation (getX() - RunSpeed, getY());
    }
    public boolean OnGround()
    {
        Actor under = getOneObjectAtOffset (0, getImage().getHeight()/2, Obstacles.class);
        return under != null;
    }
    public boolean AtRoof()
    {
        Actor above = getOneObjectAtOffset (0, -(getImage().getHeight()/2), Obstacles.class);
        return above != null;
    }
    public boolean ObjectRB()
    {
        Actor rightB = getOneObjectAtOffset (getImage().getWidth()/2, getImage().getWidth()/3, Obstacles.class);
        return rightB != null;
    }
    public boolean ObjectRT()
    {
        Actor rightT = getOneObjectAtOffset (getImage().getWidth()/2, -(getImage().getWidth()/3), Obstacles.class);
        return rightT != null;
    }
    public boolean ObjectLB()
    {
        Actor leftB = getOneObjectAtOffset (-(getImage().getWidth()/2), getImage().getWidth()/3, Obstacles.class);
        return leftB != null;
    }
    public boolean ObjectLT()
    {
        Actor leftT = getOneObjectAtOffset (-(getImage().getWidth()/2), -(getImage().getWidth()/3), Obstacles.class);
        return leftT != null;
    }
    public boolean InGround()
    {
        Actor inground = getOneObjectAtOffset (0, (getImage().getHeight()/2)-5, Obstacles.class);
        return inground != null;
    }
    public boolean OnSpike()
    {
        Actor onspike = getOneObjectAtOffset (0, (getImage().getHeight()/2), Spikes.class);
        return onspike != null;
    }
    public void MoveFromGround()
    {
        if(InGround())
        {
            setLocation (getX(), getY() - 5);
        }
    }
    public void HitRoof()
    {
        if(AtRoof())
        {
            FallSpeed = 0;
            Gravity();
        }
    }
    public void NeedGravity()
    {
        if(OnGround())
        {
            FallSpeed = 0;
        }    
        else
        {
            Gravity();
        }
    }
    public void Gravity()
    {
        setLocation (getX(), getY() + FallSpeed);
        FallSpeed = FallSpeed + FallAcceleration;
        if (FallSpeed == 5)
        {
            FallSpeed = 5;
        }
    }
    public void Jump()
    {
        FallSpeed = -12;
        Gravity();
    }
    public void SpikeDeath()
    {
        if (OnSpike())
        {   
            HitCounter = HitCounter + 1;
            setLocation (74, 401);
            LoseHealth();
        }
        else if (HitCounter == 3)
        {
            Greenfoot.setWorld(new YouLose());
        }
    }
    public void LoseHealth()
    {
        Level1 Level1World = (Level1) getWorld();
        Health health = Level1World.getHealth();
        health.TakeDamage(1);
    }
}
danpost danpost

2020/4/30

#
Please copy/paste the entire error output on the terminal.
Evstar14 Evstar14

2020/4/30

#
java.lang.NullPointerException at Player.LoseHealth(Player.java:221) at Player.SpikeDeath(Player.java:210) at Player.act(Player.java:28) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2020/4/30

#
Okay, the problem is that Health is null, which is from your Level1 class. Please post its codes.
Evstar14 Evstar14

2020/4/30

#
This is Health:
import greenfoot.*;
public class Health extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private int TotalHealth;
    public Health()
    {
        image1 = new GreenfootImage("3 Heart.png");
        image2 = new GreenfootImage("2 Heart.png");
        image3 = new GreenfootImage("1 Heart.png");
        image4 = new GreenfootImage("0 Heart.png");
        TotalHealth = 0;
    }
    public void act() 
    {
        if (TotalHealth == 0)
        {
            setImage (image1);
        }
        GreenfootImage image = getImage();
        image.scale(100,36);
    }   
    public void TakeDamage(int amount)
    {
        TotalHealth += amount;
        if (TotalHealth == 1)
        {
            setImage (image2);
        }
        else if (TotalHealth == 2)
        {
            setImage (image3);
        }
        else if (TotalHealth == 3)
        {
            setImage (image4);
        }
    }
}
This is Level1:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends World
{
    private Health CurrHealth;
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        super(1100, 450, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        BorderHorizontal borderHorizontal = new BorderHorizontal();
        addObject(borderHorizontal,550,10);
        BorderHorizontal borderHorizontal2 = new BorderHorizontal();
        addObject(borderHorizontal2,550,440);
        BorderVertical borderVertical = new BorderVertical();
        addObject(borderVertical,1090,225);
        BorderVertical borderVertical2 = new BorderVertical();
        addObject(borderVertical2,10,225);
        Vertical50 vertical50 = new Vertical50();
        addObject(vertical50,146,423);
        Vertical100 vertical100 = new Vertical100();
        addObject(vertical100,263,380);
        Vertical100 vertical1002 = new Vertical100();
        addObject(vertical1002,205,408);
        Horizontal100 horizontal100 = new Horizontal100();
        addObject(horizontal100,379,299);
        Horizontal200 horizontal200 = new Horizontal200();
        addObject(horizontal200,568,240);
        Vertical100 vertical1003 = new Vertical100();
        addObject(vertical1003,741,69);
        Vertical50 vertical502 = new Vertical50();
        addObject(vertical502,739,269);
        Vertical50 vertical503 = new Vertical50();
        addObject(vertical503,741,409);
        Horizontal50 horizontal50 = new Horizontal50();
        addObject(horizontal50,763,264);
        Horizontal100 horizontal1002 = new Horizontal100();
        addObject(horizontal1002,880,200);
        Vertical250 vertical250 = new Vertical250();
        addObject(vertical250,940,230);
        Horizontal50 horizontal502 = new Horizontal50();
        addObject(horizontal502,1072,367);
        Horizontal50 horizontal503 = new Horizontal50();
        addObject(horizontal503,968,294);
        Horizontal50 horizontal504 = new Horizontal50();
        addObject(horizontal504,1065,231);
        Horizontal50 horizontal505 = new Horizontal50();
        addObject(horizontal505,967,163);
        Player player = new Player();
        addObject(player,74,401);
        Spike1 spike1 = new Spike1();
        addObject(spike1,177,412);
        Spike1 spike12 = new Spike1();
        addObject(spike12,235,412);
        Spike1 spike13 = new Spike1();
        addObject(spike13,449,412);
        Spike1 spike14 = new Spike1();
        addObject(spike14,539,412);
        Health CurrHealth = new Health();
        addObject(CurrHealth,76,41);
    }
    public Health getHealth()
    {
        return CurrHealth;
    }
}
danpost danpost

2020/4/30

#
CurrHealth on line 76 is not the same as that on line 81. Line 76 declares a new local variable that, within the method, overshadows (because of equivalent naming) the field declared on line 11. Drop the first word on line 76 and it will then refer to the previously declared field.
Evstar14 Evstar14

2020/4/30

#
Thanks soooo much for the quick solution! I have spent almost 3 hours trying to solve that, but i had no idea what the actual problem was. Thankyou
You need to login to post a reply.