[Solved] java.lang.EnumConstantNotPresentException

Pre-requisite : Java: Enumeration Handling

This exception EnumConstantNotPresentException thrown when an application tries to access an enum constant by name and the enum type contains no constant with  the specified name. This exception can be thrown by the API used to read annotations reflectively.

Example :

In this below example trying to read annotation by using reflection APIs. Here if api attempt to read an enum-valued member will result in a EnumConstantNotPresentException if the enum constant in the annotation is no longer present in the enum type. Compile everything and run ReadAnnotation. You will got C.

// TestEnum.java
public enum TestEnum {
    A, B, C;

// TestAnnotation.java
import java.lang.annotation.*;

public @interface TestAnnotation {
    TestEnum value();

// TestClass.java
public class TestClass {


// ReadAnnotation.java
public class ReadAnnotation {
    public static void main(String[] args) {

To generate this exception, now remove the C from the TestEnum and recompile only TestEnum class preserving other classes as is. If you launch ReadAnnotation now, you will get:

Exception in thread "main" java.lang.EnumConstantNotPresentException: TestEnum.C
    at sun.reflect.annotation.EnumConstantNotPresentExceptionProxy.generateException(Unknown Source)
    at sun.reflect.annotation.AnnotationInvocationHandler.invoke(Unknown Source)
    at com.sun.proxy.$Proxy1.value(Unknown Source)
    at ReadAnnotation.main(ReadAnnotation.java:4)

References :


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s