Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason for java.lang.IllegalArgumentException: No enum const class even though iterating through values() works just fine?

Tags:

java

enums

This question is basically an extension of my previous question . I asked the previous question to be sure that the Enum constants are populated when the class loads . Here's is my class again with the addition of a simple method getByName :

public enum PropName {    CONTENTS("contents"),   USE_QUOTES("useQuotes"),   ONKEYDOWN("onkeydown"),   BROWSER_ENTIRE_TABLE("browseEntireTable"),   COLUMN_HEADINGS("columnHeadings"),   PAGE_SIZE("pageSize"),   POPUP_TITLE("popupTitle"),   FILTER_COL("filterCol"),   SQL_SELECT("sqlSelect"),   ;    private String name;    private PropName(String name) {     this.name = name;   }    public String getName() {     return name;   }    public static PropName getByName(String name){     return   PropName.valueOf(name);   } } 

A call to the method getByName("columnHeadings") is throwing java.lang.IllegalArgumentException: No enum const class labware.web.component.limsgrid.PropName.columnHeadings but if I replace this method with the following code it just works .

 public static PropName getByName(String name){     for(PropName prop : values()){       if(prop.getName().equals(name)){         return prop;       }     }      throw new IllegalArgumentException(name + " is not a valid PropName");   } 

Any ideas as to what I am doing wrong here ?

like image 725
Geek Avatar asked Sep 28 '12 12:09

Geek


People also ask

What is Java Lang IllegalArgumentException?

IllegalArgumentException is a Java exception indicating that a method has received an argument that is invalid or inappropriate for this method's purposes.

What is Java enum?

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comma.

What does enum valueOf return?

valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)


2 Answers

Enum.valueOf() only checks the constant name, so you need to pass it "COLUMN_HEADINGS" instead of "columnHeadings". Your name property has nothing to do with Enum internals.


To address the questions/concerns in the comments:

The enum's "builtin" (implicitly declared) valueOf(String name) method will look up an enum constant with that exact name. If your input is "columnHeadings", you have (at least) three choices:

  1. Forget about the naming conventions for a bit and just name your constants as it makes most sense: enum PropName { contents, columnHeadings, ...}. This is obviously the most convenient.
  2. Convert your camelCase input into UPPER_SNAKE_CASE before calling valueOf, if you're really fond of naming conventions.
  3. Implement your own lookup method instead of the builtin valueOf to find the corresponding constant for an input. This makes most sense if there are multiple possible mappings for the same set of constants.
like image 197
Costi Ciudatu Avatar answered Sep 21 '22 22:09

Costi Ciudatu


That's because you defined your own version of name for your enum, and getByName doesn't use that.

getByName("COLUMN_HEADINGS") would probably work.

like image 33
Guillaume Avatar answered Sep 20 '22 22:09

Guillaume