Enum valueOf IllegalArgumentException: no class enum const

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 in which it throws an error:

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

I get

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

the example string contains an array of strings.

Now I'm just confused, I don’t know what could be wrong.

+5
source share
2 answers

Enumeration constants are case sensitive, so make sure you are, in fact, lowercase. In addition, I would suggest that you trim()enter as well to make sure that it does not have scroll spaces:

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

For reference, here is a working 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;
        }
    }
}

Conclusion:

Property 1
+16
source

,

public enum SortType {

    PRICE_ASC("price_asc"),
    PRICE_DESC("price_desc"),
    MODEL_ASC("model_asc"),
    MODEL_DESC("model_desc");

    private String name;

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

    public String getName() {
        return name;
    }

    static public SortType lookup(String id, SortType defaultValue) {
        try {
            return SortType.valueOf(id);
        } catch (IllegalArgumentException ex) {
            return defaultValue;
        }
    }
}
0

All Articles