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 8358319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:45:29+00:00 2026-06-09T10:45:29+00:00

The following code fails with a NullPointerException in main (map==null) . The issue occurs

  • 0

The following code fails with a NullPointerException in main (map==null).
The issue occurs only if I define 2001 or more Enum constants, 2000 work fine.

Why isn’t the static code block not executed?

Do we hit any silent limit of the compiler (no warnings, no errors) or JVM?

The compiled class file exceeds 172KB,

import java.util.HashMap;

public enum EnumTest {
    E(1),E(2),...,E(2001);

    private static HashMap<Integer, EnumTest>   map = new HashMap<Integer, EnumTest>();

    static {

        for ( EnumTest f : EnumTest.values() ) {
            map.put( (int) f.id, f );
        }
    }
    short id;

    private EnumTest(int id) {
        this.id = (short) id;
    };

    public short getId() {
        return id;
    }

    public static final EnumTest fromInt(int id) {
        EnumTest e = map.get( id );
        if ( e != null ) {
            return e;
        }
        throw new IllegalArgumentException( "" + id );
    }

    public static void main(String[] args) {
        System.out.println( "size:" + map.size() );
    }
}

Runtime Environment:

java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode)

also happens with:

java version "1.6.0_32" Java(TM) SE Runtime Environment (build
1.6.0_32-b05) Java HotSpot(TM) Client VM (build 20.7-b02, mixed mode, sharing)
  • 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-09T10:45:31+00:00Added an answer on June 9, 2026 at 10:45 am

    These kinds of problems come from the fact that some (often compiler-generated) initializer code exceeds 65536 bytes of byte code. A single method can not contain more than that many bytes of bytecode to be executed (due to restrictions in the class file format).

    A common source of problems like this are large arrays like this:

    byte someBytes = { 1, 2, 3, ..., someBigValue };
    

    The problem here is that such fields are actually initialized with someBigValue assignment statements in a generated initializer (constructor or static initializer).

    Enum values are actually initialized in a similar fashion.

    Given the following enum class:

    public enum Foo {
      CONSTANT(1);
    
      private Foo(int i) {
      }
    }
    

    We look at the output of javap -v and see the following code block:

      static {};
        flags: ACC_STATIC
        Code:
          stack=5, locals=0, args_size=0
             0: new           #4                  // class Foo
             3: dup
             4: ldc           #7                  // String CONSTANT
             6: iconst_0
             7: iconst_1
             8: invokespecial #8                  // Method "<init>":(Ljava/lang/String;II)V
            11: putstatic     #9                  // Field CONSTANT:LFoo;
            14: iconst_1
            15: anewarray     #4                  // class Foo
            18: dup
            19: iconst_0
            20: getstatic     #9                  // Field CONSTANT:LFoo;
            23: aastore
            24: putstatic     #1                  // Field $VALUES:[LFoo;
            27: return
    

    As you can see there are quite a lot of bytecode operations that handle instantiating CONSTANT with the correct values. If you have many such enum values, then the size of that static initializer block can easily exceed 64k bytes of code and thus make the class uncompilable.

    A possible workaround is to reduce the size of the initializing code by reducing the number of arguments (for example by calculating the number passed in based on the index of the enum value instead of using an argument). That might just give you enough wiggle room to extend this a bit further.

    Alternatively you could try splitting your enum into several enums connected by implementing a common interface. The enums could be grouped by area/intention/category/…:

    public interface MessageType {
      int getId();
    }
    
    public enum ConnectionMessage implements MessageType {
      INIT_CONNECTION(1),
      LOGIN(2),
      LOGOUT(3),
      CLOSE_CONNECTION(4);
    
      // getId code, constructor, ...
    }
    
    public enum FrobnicationMessage implements MessageType {
      FROBNICATE_FOO(5),
      FROBNICATE_BAR(6),
      DEFROB_FOO(7),
      DEFROB_BAR(8),
      ...
    
      // getId code, constructor, ...
    }
    

    I’m assuming that the enum values are actually referenced somewhere in your code and not just pure value holders, if they only hold values and individual values are not treated differently in your code, then replacing them with a single class instantiated once per data item stored in a central resource is probably the best approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code fails in 'Evaluate' with: This expression was expected to have type
The following code fails to compile #include <iostream> #include <cmath> #include <complex> using namespace
The following code fails to run on Python 2.5.4: from matplotlib import pylab as
Can anyone tell me why the following code fails to submit the form into
I can't figure out why the following code fails: # test.ps1 `$args: ($args) `$args
I have the following code that fails when i run it. my .h file:
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
The following code snippet on SQL server 2005 fails on the ampersand '&': select
If I use strict mode the following code does not work. It fails on
The following code fails to compile on g++ 4.6.1: template<class Base> struct GetBase {

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.