What is the reason for java.lang.IllegalArgumentException: No enum const class even though iterating through values() works just fine?
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:
- 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. - Convert your camelCase input into UPPER_SNAKE_CASE before calling
valueOf
, if you're really fond of naming conventions. - 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.
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.
Instead of defining: COLUMN_HEADINGS("columnHeadings")
Try defining it as: COLUMNHEADINGS("columnHeadings")
Then when you call getByName(String name) method
, call it with the upper-cased String like this: getByName(myStringVariable.toUpperCase())
I had the same problem as you, and this worked for me.