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

2019/5/8

Invaders (actors) are null when get by a getter in a world method.

Notted Notted

2019/5/8

#
Right on the tin. The world is not detecting that there are any Invader actors in the world. This causes Null Pointer Exceptions when trying to do something with those actors, like reducing health, speed, etc. Getter for Invader:
private Invader inv;
    public Invader getInvaders()
    {
        return inv;
    }
What I'm trying to do with the Invader:
int invHP = invWorld.getInvaders().getHP();
                if (invHP == 0)
                {
                invWorld.removeObject(invader);
                invWorld.removeObject(this);
                }
Getter for the invaders HP:
public int getHP ()
    {
        return invaderHP;
    }
Is there any way that this can be solved.
Super_Hippo Super_Hippo

2019/5/8

#
Possible problems: -- "inv" is never added to the world (which happens if you add the Invaders with "Invader inv = new Invader(); addObject(inv, …)") -- "invWorld" is never assigned the correct world (which happens if you used "InvWorld invWorld = getWorld()" outside methods or in the constructor for example) Another thing: If there are several Invaders in your world, how is the "getInvaders" method going to return the correct Invader?
Notted Notted

2019/5/8

#
1. The spawner for Invaders is this:
addObject(new Invader(Greenfoot.getRandomNumber(4) + 1), 50+i*50, 20+j*50);
Changing Invader into inv does not work. It says that inv does not exist. 2. invWorld is the correct world for the HP of the invaders. The older world getter was World world = getWorld(), which didn't work for getInvaders(), and therefore getHP(). Yes there are many invaders. Eight as of now per row.
Super_Hippo Super_Hippo

2019/5/8

#
Then "inv" will stay "null" because it is never changed. Keep the HP of the Invaders in the Invaders class.
You need to login to post a reply.