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 ?