Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum valueOf IllegalArgumentException: No enum const class

Tags:

java

enums

I have used enums in java in the past but for some reason I am getting a strange error right now. the Line of code that it is throwing the error is:

switch(ConfigProperties.valueOf(line[0].toLowerCase()){
    ...
}

I am getting a

java.lang.IllegalArgumentException: No enum const class
  allautomator.ConfigProperties.language 

in the example line is an array of Strings.

I am just really confused right now, I do not know what could possibly be wrong.

like image 655
Ryan Sullivan Avatar asked May 19 '11 18:05

Ryan Sullivan


1 Answers

The enum constants are case sensitive, so make sure you're constants are indeed lower case. Also, I would suggest that you trim() the input as well to make sure no leading / trailing white-space sneak in there:

ConfigProperties.valueOf(line[0].toLowerCase().trim())

For reference, here is a working sample program containing your line:

enum ConfigProperties { prop1, prop2 }

class Test {
    public static void main(String[] args) {

        String[] line = { "prop1" };

        switch (ConfigProperties.valueOf(line[0].toLowerCase())) {
        case prop1: System.out.println("Property 1"); break;
        case prop2: System.out.println("Property 2"); break;
        }
    }
}

Output:

Property 1
like image 135
aioobe Avatar answered Oct 14 '22 14:10

aioobe