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

2020/3/15

java.lang.NullPointerException

ezio4864 ezio4864

2020/3/15

#
Hello, I made 2 actor classes for the 2 levels I made (a first level and a boss level) I also made 2 hearts classes to represent the lives of the 2 characters. The code for the hearts class is this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Hearts extends Actor
{
    //arraylist to cycle between the hearts of the knight
    GreenfootImage[] hearts = { new GreenfootImage("healthbar0.png"),
            new GreenfootImage("healthbar1.png"),
            new GreenfootImage("healthbar2.png"),
            new GreenfootImage("healthbar3.png")};
    int health = 3;
    public void act() 
    {
        // Add your action code here.
    }    

    public Hearts()
    {
        //method used to call on update image
        updateImage();
    }

    private void updateImage()
    {
        //sets # of hearts depending on what image the arraylist holds
        setImage(hearts[health]);
    }

    public void adjustHearts(int adjustmentValue)
    {
        //conditions for the hearts, makes the images cycle between themselves, calls on the DEAD banner and stops game on 0 hearts
        health += adjustmentValue;
        if (health > 3) 
        {
            health = 3;
        }

        if (health == 0) 
        {
            getWorld().addObject(new DEAD(this), 400, 300);
            updateImage();
            Greenfoot.stop();
        }

        else 
        {
            updateImage();
        }
    }
}
When an enemy such as an eye or a fireball touches either of the characters, it will send an adjustment value to their respective Hearts classes. The code for the contact between the fireball and the character is this:
public void burn(){
        //method to damage KNIGHT on contact with character
        if (isTouching(KNIGHT.class)){

            MyWorld worldsubclass = (MyWorld) getWorld();
            worldsubclass.hearts.adjustHearts(-1);
        }
        if (isTouching(KNIGHT2.class)){
            BossLevel bosslevel = (BossLevel) getWorld();
            bosslevel.hearts2.adjustHearts(-1);
        }
    }
The first level progresses smoothly, the heart images cycle at the lower left portion of the screen and it loads up to the next level, but when the character hits a fireball shot by the boss monster, the game stops and this error appears:
java.lang.NullPointerException at Hearts2.adjustHearts(Hearts2.java:45) at FIREBALLl.burn(FIREBALLl.java:53) at FIREBALLl.act(FIREBALLl.java:23)
Help is very much appreciated!
ezio4864 ezio4864

2020/3/15

#
ezio4864 wrote...
I made 2 actor classes for the 2 levels I made (a first level and a boss level) I also made 2 hearts classes to represent the lives of the 2 characters.
The first character is the character of the first level and and second character is the character of the other. I guess I'll also send the code for them and they're completely identical except for the class names:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class KNIGHT here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class KNIGHT extends Actor
{
    //various .gif files used for the diff animations of the knight
    GifImage WALKRIGHT = new GifImage("WALK RIGHT.gif");

    GifImage WALKLEFT = new GifImage("WALK LEFT.gif");

    GifImage STANDRIGHT = new GifImage("STAND RIGHT.gif");

    GifImage STANDLEFT = new GifImage("STAND LEFT.gif");
    
    public void act() 
    {
        movement();
    }    

    public void movement()
    {
        //variables for movement of knight
        int dx = 0, dy = 0;
        
        setImage(STANDRIGHT.getCurrentImage());
        if (Greenfoot.isKeyDown("W")){
            dy = -3;
            setImage(WALKLEFT.getCurrentImage());
        }

        if (Greenfoot.isKeyDown("A")){
            dx = -3;
            setImage(WALKLEFT.getCurrentImage());
        }

        if (Greenfoot.isKeyDown("S")){
            dy = 3; 
            setImage(WALKLEFT.getCurrentImage());
        }

        if (Greenfoot.isKeyDown("D")){
            dx = 3;
            setImage(WALKRIGHT.getCurrentImage());
        }
        setLocation(getX()+dx, getY()+dy);

        //will prevent the knight from passing through the wall
        if (isTouching(WALL.class))
        {
            setLocation(getX()-dx, getY()-dy);
        }
    }
}
danpost danpost

2020/3/15

#
Show level 2 world subclass codes.
ezio4864 ezio4864

2020/3/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class BossLevel extends World
{
    public Hearts2 hearts2 = new Hearts2();
    public BossLevel()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 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()
    {
        WALL wALL = new WALL();
        addObject(wALL,779,343);
        WALL wALL2 = new WALL();
        addObject(wALL2,778,380);
        WALL wALL3 = new WALL();
        addObject(wALL3,778,419);
        WALL wALL4 = new WALL();
        addObject(wALL4,778,460);
        WALL wALL5 = new WALL();
        addObject(wALL5,778,498);
        WALL wALL6 = new WALL();
        addObject(wALL6,779,540);
        WALL wALL7 = new WALL();
        addObject(wALL7,777,581);
        WALL wALL8 = new WALL();
        addObject(wALL8,741,579);
        WALL wALL9 = new WALL();
        addObject(wALL9,703,580);
        WALL wALL10 = new WALL();
        addObject(wALL10,671,581);
        WALL wALL11 = new WALL();
        addObject(wALL11,635,581);
        WALL wALL12 = new WALL();
        addObject(wALL12,601,581);
        WALL wALL13 = new WALL();
        addObject(wALL13,565,580);
        WALL wALL14 = new WALL();
        addObject(wALL14,528,581);
        WALL wALL15 = new WALL();
        addObject(wALL15,497,579);
        GATE gATE = new GATE();
        addObject(gATE,400,566);
        gATE.setLocation(384,542);
        KNIGHT kNIGHT = new KNIGHT();
        addObject(kNIGHT,384,542);
        removeObject(kNIGHT);
        removeObject(gATE);
        WALL wALL16 = new WALL();
        addObject(wALL16,459,579);
        WALL wALL17 = new WALL();
        addObject(wALL17,422,579);
        WALL wALL18 = new WALL();
        addObject(wALL18,383,580);
        WALL wALL19 = new WALL();
        addObject(wALL19,345,582);
        WALL wALL20 = new WALL();
        addObject(wALL20,307,581);
        WALL wALL21 = new WALL();
        addObject(wALL21,271,581);
        WALL wALL22 = new WALL();
        addObject(wALL22,236,581);
        WALL wALL23 = new WALL();
        addObject(wALL23,200,579);
        WALL wALL24 = new WALL();
        addObject(wALL24,163,581);
        WALL wALL25 = new WALL();
        addObject(wALL25,130,580);
        WALL wALL26 = new WALL();
        addObject(wALL26,94,580);
        WALL wALL27 = new WALL();
        addObject(wALL27,56,581);
        WALL wALL28 = new WALL();
        addObject(wALL28,22,578);
        WALL wALL29 = new WALL();
        addObject(wALL29,23,541);
        WALL wALL30 = new WALL();
        addObject(wALL30,22,502);
        WALL wALL31 = new WALL();
        addObject(wALL31,20,465);
        WALL wALL32 = new WALL();
        addObject(wALL32,21,429);
        WALL wALL33 = new WALL();
        addObject(wALL33,21,391);
        WALL wALL34 = new WALL();
        addObject(wALL34,19,355);
        WALL wALL35 = new WALL();
        addObject(wALL35,18,317);
        WALL wALL36 = new WALL();
        addObject(wALL36,21,281);
        WALL wALL37 = new WALL();
        addObject(wALL37,21,244);
        WALL wALL38 = new WALL();
        addObject(wALL38,20,206);
        WALL wALL39 = new WALL();
        addObject(wALL39,21,170);
        WALL wALL40 = new WALL();
        addObject(wALL40,21,131);
        WALL wALL41 = new WALL();
        addObject(wALL41,22,93);
        WALL wALL42 = new WALL();
        addObject(wALL42,20,51);
        WALL wALL43 = new WALL();
        addObject(wALL43,23,21);
        WALL wALL44 = new WALL();
        addObject(wALL44,58,18);
        WALL wALL45 = new WALL();
        addObject(wALL45,97,19);
        WALL wALL46 = new WALL();
        addObject(wALL46,132,18);
        WALL wALL47 = new WALL();
        addObject(wALL47,166,20);
        WALL wALL48 = new WALL();
        addObject(wALL48,203,21);
        WALL wALL49 = new WALL();
        addObject(wALL49,239,19);
        WALL wALL50 = new WALL();
        addObject(wALL50,271,19);
        WALL wALL51 = new WALL();
        addObject(wALL51,310,20);
        WALL wALL52 = new WALL();
        addObject(wALL52,346,20);
        WALL wALL53 = new WALL();
        addObject(wALL53,383,19);
        WALL wALL54 = new WALL();
        addObject(wALL54,419,21);
        WALL wALL55 = new WALL();
        addObject(wALL55,460,19);
        WALL wALL56 = new WALL();
        addObject(wALL56,496,19);
        WALL wALL57 = new WALL();
        addObject(wALL57,531,19);
        WALL wALL58 = new WALL();
        addObject(wALL58,570,17);
        WALL wALL59 = new WALL();
        addObject(wALL59,610,18);
        WALL wALL60 = new WALL();
        addObject(wALL60,649,21);
        WALL wALL61 = new WALL();
        addObject(wALL61,687,19);
        WALL wALL62 = new WALL();
        addObject(wALL62,725,20);
        WALL wALL63 = new WALL();
        addObject(wALL63,763,19);
        wALL63.setLocation(781,20);
        WALL wALL64 = new WALL();
        addObject(wALL64,781,20);
        WALL wALL65 = new WALL();
        addObject(wALL65,777,60);
        WALL wALL66 = new WALL();
        addObject(wALL66,779,99);
        WALL wALL67 = new WALL();
        addObject(wALL67,776,140);
        WALL wALL68 = new WALL();
        addObject(wALL68,779,178);
        WALL wALL69 = new WALL();
        addObject(wALL69,777,217);
        WALL wALL70 = new WALL();
        addObject(wALL70,779,253);
        WALL wALL71 = new WALL();
        addObject(wALL71,776,287);
        WALL wALL72 = new WALL();
        addObject(wALL72,779,319);
        WALL wALL73 = new WALL();
        addObject(wALL73,781,354);
        WALL wALL74 = new WALL();
        addObject(wALL74,777,391);
        WALL wALL75 = new WALL();
        addObject(wALL75,778,429);
        wALL4.setLocation(778,467);
        WALL wALL76 = new WALL();
        addObject(wALL76,778,467);
        BM bM = new BM();
        addObject(bM,180,114);
        bM.setLocation(407,277);
        wALL26.setLocation(96,575);
        Hearts2 hearts2 = new Hearts2();
        addObject(hearts2,96,575);
        wALL26.setLocation(70,580);
        KNIGHT2 kNIGHT2 = new KNIGHT2();
        addObject(kNIGHT2,106,103);
    }
}
danpost danpost

2020/3/15

#
Remove the first word ("Hearts2") from line 184.
ezio4864 ezio4864

2020/3/15

#
Thank you so much! Everything works perfectly now. I really appreciate it!
You need to login to post a reply.