Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:21:25+00:00 2026-06-12T11:21:25+00:00

I was trying an alternate way of doing some code I already have that

  • 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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:21:26+00:00Added an answer on June 12, 2026 at 11:21 am

    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>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a little experiment, trying to alternate background colours for nested divs. This
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I have an Object[] array, and I am trying to find the ones that
I have been trying to grab a file from an alternate git branch and
I have some concurrent code which has an intermittent failure and I've reduced the
I have a class that holds a mysqli instance (this to avoid spreading database
I have some special cases that I need to test for in django. I
I'm not sure I'm doing this the right way, so I'm open to other
I am trying to implement some tables that mimic inheritance in a relational (sqlite)
i have been looking around and haven't quite found anything on doing this and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.