Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this short article, we'll take a close look at the exception “IllegalArgumentException: No enum const class“.

First, we'll understand the leading cause behind this exception. Then, we'll see how to reproduce it using a practical example, and finally learn how to fix it.

2. The Cause

Before diving deep into the details, let's understand what the exception and its stack trace mean.

Typically, IllegalArgumentException occurs when we pass an illegal or inappropriate value to a method.

No enum const class” tells us that there's no constant with the given name in the specified enum type.

So, the most typical cause of this exception is usually using an invalid enum constant as a method argument.

3. Reproducing the Exception

Now that we know what the exception means, let's see how to reproduce it using a practical example.

For instance, let's consider the Priority enum:

public enum Priority {

    HIGH("High"), MEDIUM("Medium"), LOW("Low");

    private String name;

    Priority(String name) {
        this.name = name;
    }

    public String getPriorityName() {
        return name;
    }

}

As we can see, our enum has a private field name that denotes the name of each Priority constant.

Next, let's create a static method to help us get a Priority constant by its name:

public class PriorityUtils {

    public static Priority getByName(String name) {
        return Priority.valueOf(name);
    }

    public static void main(String[] args) {
        System.out.println(getByName("Low"));
    }

}

Now, if we execute the PriorityUtils class, we get an exception:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.baeldung.exception.noenumconst.Priority.Low
    at java.lang.Enum.valueOf(Enum.java:238)
    at com.baeldung.exception.noenumconst.Priority.valueOf(Priority.java:1)
....

Looking at the stack trace, getByName(String name) fails with the exception because the built-in method Enum.valueOf() fails to find a Priority constant with the given name “Low“.

Enum.valueOf() accepts only a string that must match exactly the identifier used to declare the constant in the enum. In other words, it accepts only HIGH, MEDIUM, and LOW as arguments. Since it doesn't know about the name property, it throws IllegalArgumentException when we pass it the value “Low”.

So, let's confirm this using a test case:

@Test
void givenCustomName_whenUsingGetByName_thenThrowIllegalArgumentException() {
    assertThrows(IllegalArgumentException.class, () -> PriorityUtils.getByName("Low"));
}

4. The Solution

The easiest solution would be converting the custom name to uppercase before passing it to the Enum.valueOf() method.

That way, we make sure that the passed string matches exactly the constant's names which are in uppercase.

Now, let's see it in action:

public static Priority getByUpperCaseName(String name) {
    if (name == null || name.isEmpty()) {
        return null;
    }

    return Priority.valueOf(name.toUpperCase());
}

To avoid any NullPointerException or unwanted behavior, we added a check to make sure that the given name is not null and that it's not empty.

Lastly, let's add some test cases to confirm that everything works as excepted:

@Test
void givenCustomName_whenUsingGetByUpperCaseName_thenReturnEnumConstant() {
    assertEquals(Priority.HIGH, PriorityUtils.getByUpperCaseName("High"));
}

As we can see, we successfully get Priority.HIGH using the custom name High.

Now, let's check what happens when we pass a null or an empty value:

@Test
void givenEmptyName_whenUsingGetByUpperCaseName_thenReturnNull() {
    assertNull(PriorityUtils.getByUpperCaseName(""));
}

@Test
void givenNull_whenUsingGetByUpperCaseName_thenReturnNull() {
    assertNull(PriorityUtils.getByUpperCaseName(null));
}

As we see above, the method returns null indeed.

5. Conclusion

In this short tutorial, we discussed in detail what causes Java to throw the exception “IllegalArgumentException: No enum const class“.

Along the way, we learned how to produce the exception and how to fix it using practical examples.

As always, the full source code of the examples is available over on GitHub.

 

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments