Laruka
Laruka

Reputation: 33

Enum class method gives nullpointer

Hey I am trying to work with an enum class, but this class keeps giving me nullpointers if I try to call the getLocation method... Any one knows what is up?

Enum class:

    public enum ZulrahLocation {

    CENTER(ZulrahScript.TILE_CENTER),
    WEST(ZulrahScript.TILE_WEST),
    SOUTH(ZulrahScript.TILE_SOUTH),
    EAST(ZulrahScript.TILE_EAST);

    private InstanceTile location;

    ZulrahLocation(final InstanceTile location)
    {
    this.location = location;
    }

    public Position getLocation()
    {
        return location.toTile();
    }
    }

Class that loops through enum class: nullpointer at the log z1.getlocation()

     for (ZulrahLocation zl : ZulrahLocation.values())
     {
     sI.log(String.valueOf(zl.getLocation()) + " Location of z1");
     }

If somebody is wondering I am setting all the tiles in the main class which happens here:

    public void onStart () {
    TILE_CENTER                 = new InstanceTile(164, 99, 0,this);
    TILE_WEST                   = new InstanceTile(154, 97, 0,this);
    TILE_SOUTH                  = new InstanceTile(164, 88, 0,this);
    TILE_EAST                   = new InstanceTile(174, 97, 0,this);
    }

If you need additional information please let me know.

Upvotes: 1

Views: 53

Answers (1)

Wrokar
Wrokar

Reputation: 963

When your program first starts, the ZulrahLocation enum values are initialized with the current values of your ZulrahScript tiles. Because you are defining them in a method (onStart) that needs to be called, the initial values that are used are actually null.

In your ZulrahScript class, you may want to declare and define the variables on the same line.

public class ZulrahScript {
    ...
    public static InstanceTile TILE_CENTER = new InstanceTile(164, 99, 0,this);
    ...
}

Upvotes: 1

Related Questions