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

2014/8/29

NullPointer exception!

K_wow K_wow

2014/8/29

#
I'm trying to obtain a boolean in an array from the world, then convert it to a string, like this:
    private String getValue()
    {
        if (intOrBool == 0)
        {
            return "" + board.intSettings[intArrayValue];
        }
        else if (intOrBool == 1)
        {
            return "" + board.boolSettings[boolArrayValue];
        }
        else
        {
            return "";
        }
    }
Here's the world code:
    public boolean[] boolSettings = {
        true //attack effects
    };
However I get a NullPointerException when attempting to run this code, and it points me to this line:
return "" + board.boolSettings[boolArrayValue];
Does anyone know what I'm doing wrong? Would you like me to post more code?
danpost danpost

2014/8/29

#
How is 'board' declared and set in the class?
K_wow K_wow

2014/8/29

#
Like this:
    private Board board;
    
    public void AddedToWorld(World world)
    {
        board = (Board)world;
        if (intOrBool == 0)
        {
            name = board.intSettingNames[intArrayValue];
        }
        else if (intOrBool == 1)
        {
            name = board.boolSettingNames[boolArrayValue];
        }
    }
danpost danpost

2014/8/29

#
It may be that the 'getValue' method is being called before the actor is placed in the world. Try putting the following as the first line (inserted at line 3 in the 'getValue' method):
if (getWorld() == null) return "";
K_wow K_wow

2014/8/30

#
Nope, that didn't work. Ok... It turns out I spelt addedToWorld with a capital A...
You need to login to post a reply.