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

  • Home
  • SEARCH
  • 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 6622099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:21:44+00:00 2026-05-25T21:21:44+00:00

Given the code below, import java.util.Date; import java.util.HashMap; import java.util.Map; public enum SearchDataTypes {

  • 0

Given the code below,

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public enum SearchDataTypes {

  FIELD_DATATYPE_TEXT(String.class,"_t"),
  FIELD_DATATYPE_INT(Integer.class,"_i"),
  FIELD_DATATYPE_LONG(Long.class,"_l"),
  FIELD_DATATYPE_FLOAT(Float.class,"_f"),
  FIELD_DATATYPE_DOUBLE(Double.class, "_d" ),
  FIELD_DATATYPE_DATE(Date.class,"_dt");

  SearchDataTypes(final Class<?> clazz, final String affix) {
    this.affix = affix;
    this.clazz = clazz;
    getAffixMap().put(affix, this);
    getClassMap().put(clazz, this);
  }

  public String getFieldName(String objectFieldName) {
    return objectFieldName+affix;
  }

  public String getObjectFieldName(String FieldName) {
    int len = FieldName.length();
    len -= this.affix.length();
    return FieldName.substring(0, len);
  }
  public static SearchDataTypes findByAffix(String affix) {
    SearchDataTypes obj = getAffixMap().get(affix);
    assert obj != null;
    return obj;
  }

  public static SearchDataTypes findByClass(Class<?> clazz) {
    SearchDataTypes obj = getClassMap().get(clazz);
    assert obj != null;
    return obj;
  }

  private String affix;
  private Class<?> clazz;

  private static Map<Class<?>, SearchDataTypes> classMap = new HashMap<Class<?>, SearchDataTypes>();
  private static Map<String, SearchDataTypes> affixMap = new HashMap<String, SearchDataTypes>();

  private static Map<Class<?>, SearchDataTypes> getClassMap() { return classMap; }
  private static Map<String, SearchDataTypes> getAffixMap() { return affixMap; }


}

The enum class is not getting instantiated (using the enum throws NoClassDefFoundError) because the there is NullPointerException during initialization. I assume that the JVM is thinking either map is null. But why??

How else can I implement a finder for enums? I prefer not to use java.util.EnumMap class mainly because I want to better understand the innerworking of enums.

thank you

  • 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-05-25T21:21:45+00:00Added an answer on May 25, 2026 at 9:21 pm

    Think of the enum constants as public static final members of the Java class. Like all static members, they are initialized in source order. So the constants are being initialized before the maps, thus you’ll get the null pointer exception when referencing the map in the constructors, because the map hasn’t been initialized yet.

    Though not possible with java syntax, the simple solution would be to declare the maps before the enums–but since that’s not possible, the next best thing is to just initialize the maps in a static block after the constants: e.g.

    public enum SearchDataTypes {
        ...
        FIELD_DATATYPE_DATE(Date.class,"_dt");
    
        private static final Map<String,SearchDataTypes> affixMap = new HashMap<String,SearchDataType>();
        private static final Map<Class<?>, SearchDataTypes> classMap = new HashMap<Class<?>, SearchDataTypes>()
        static {
            for (SearchDataType value : values()) {
                map.put(value.affix, value);
                map.put(value.clazz, value);
            }
        }
    
        SearchDataTypes(final Class<?> clazz, final String affix) {
            this.affix = affix;
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below program prints the Currency symbol given the ISO 4217 currency code. import java.util.*;
I have the below groovy code import java.util.regex.Matcher; import java.util.regex.Pattern; String myInput=License_All (12313) String
Running the following (example) code import java.io.*; public class test { public static void
I have three java types as defined below: Main.java: import java.util.Arrays; import java.util.List; public
Given the Java code below, what's the closest you could represent these two static
Given the code below, how would you create/implement SR.h so that it produces the
Given the code below ... Net::HTTP.start('localhost', 4000) do |http| # # usual stuff omitted
Given the code below, how do I compare a List of objects's values with
I was given the code below to disable a button on an ASP.Net page
Any ideas given the code below why the highlight is being triggered to run

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.