ApplePie
ApplePie

Reputation: 8942

NullPtrException Java using enum in Map

I was trying an alternate way of doing some code I already have that I find unelegant and I met this exception. I don't really understand what's happening, I'm still very new to Java.

I'm sorry if there is a bit much code but I don't think I can cut much more. The exception is raised on the first line of Initialize().

Here is the exact error message:

Exception in thread "main" java.lang.NullPointerException at simulationia.CritterInfo.Initialize(Critter.java:35) at simulationia.SimulationIA.main(SimulationIA.java:21)

Line 35 is the first line of Initialize(). Line 21 of SimulationIA is the call to Initialize().

// Critter.java
class CritterInfo { 
    static private Map<Object, String> enum_desc;

    public enum CRITTER_TYPE { CT_HERBIVORE, CT_CARNIVORE }
    public enum CRITTER_STATE { CS_FULL, CS_HUNGRY, CS_STARVING, CS_DEAD }

    /* ... */

    static void Initialize() {
        enum_desc.put((Object)CRITTER_TYPE.CT_HERBIVORE,    "Herbivore");
        enum_desc.put((Object)CRITTER_TYPE.CT_CARNIVORE,    "Carnivore");
        enum_desc.put((Object)CRITTER_STATE.CS_FULL,        "Full");
        enum_desc.put((Object)CRITTER_STATE.CS_HUNGRY,      "Hungry");
        enum_desc.put((Object)CRITTER_STATE.CS_STARVING,    "Starving");
        enum_desc.put((Object)CRITTER_STATE.CS_DEAD,        "Dead");
    }

    /* ... */
}   

The other file...

// SimulationIA.java
public class SimulationIA {
    public static void main(String[] args) {
        /* ... */

        CritterInfo.Initialize();

        /* ... */
    }
}

Basically what I am trying to do is to have one single map to hold all enum values without caring about its type and having to check with instanceof. Maybe this is just not doable.

Edit: I think this may have to do with the fact that I do not use actual objects, only values of the enum hence it complaining about null pointer. Is that right ? How would I go around this ?

Upvotes: 0

Views: 106

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347264

Lets start with basic...

public enum CRITTER_TYPE { CT_HERBIVORE, CT_CARNIVORE }
public enum CRITTER_STATE { CS_FULL, CS_HUNGRY, CS_STARVING, CS_DEAD }

public static void main(String[] args) {
    for (CRITTER_TYPE type : CRITTER_TYPE.values()) {
        System.out.println(type);
    }
}

Will output

CT_HERBIVORE
CT_CARNIVORE

So we know that an enum will output the "name" of the enum via it's toString method.

This is good to know, as you can make your enums descriptive.

If this isn't good enough (there's no support for spaces, or you want something a little more friendly), you could do something like...

public enum CRITTER_TYPE {
    CT_HERBIVORE("Herbivore"),
    CT_CARNIVORE("Carnivore");

    private String description;
    private CRITTER_TYPE(String desc) {
        description = desc;
    }

    public String getDescription() {
        return description;
    }

}

public static void main(String[] args) {
    for (CRITTER_TYPE type : CRITTER_TYPE.values()) {
        System.out.println(type.getDescription());
    }
}

Which outputs

Herbivore
Carnivore

enum in Java is a special type of object, you might like to have a read through Enum Types for some more information

Upvotes: 2

LanguagesNamedAfterCofee
LanguagesNamedAfterCofee

Reputation: 5952

Based on your comment, "I just want to be able to have a text description matched to every enum value":

You can take advantage of Java's enums:

public enum Enum {

    DOG("Dogs are cool"),
    CAT("Dogs are better"),
    GOAT("Free milk");

    private final String desc;

    private Enum(String desc) {
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }    

}

Upvotes: 2

Jeremy Roman
Jeremy Roman

Reputation: 16355

You never create the Map, so put is called on null.

You should do something like (for example):

static private Map<Object, String> enum_desc = new HashMap<Object, String>();

Upvotes: 5

Related Questions