0

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 ?

2
  • To be blunt, that's terrible design. If you elaborate on what you're trying to accomplish, we can help you improve it. Commented Oct 8, 2012 at 1:10
  • Sure. I don't know any better. Basically I just want to be able to have a text description matched to every enum value. This is not really an issue in what I am trying to do. I'm just trying to learn. Commented Oct 8, 2012 at 1:13

3 Answers 3

5

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>();
Sign up to request clarification or add additional context in comments.

3 Comments

Dumb mistake ! I also notice that I use an abstract type instead of a instantiable type.
@Alexandre Declaring the variable using an abstract type is usually a good idea.
Giving checkmark because this solves the problem I listed (even though I will take advice from other posts on how to do what I want to achieve more properly).
2

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;
    }    

}

1 Comment

Nifty. I didn't know about that.
2

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

5 Comments

I read that page earlier today but I did not find it particularly useful to be honest. Google has failed me on this one.
So the public enum Planet example didn't trigger something, cause that's all we've demonstrated here.
I'm not used to using enums as classes so I didn't think of using it this way. No need to be a jerk about it.
Sorry, wasn't meant as a grip, more a surprise, my apologies if I offended, enums still do me head in to :P
No worries. A little bit of misunderstanding that's all !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.