9

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.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Ryan Sullivan
  • 447
  • 2
  • 6
  • 15
  • 2
    and what does your enum look like? – Bozho May 19 '11 at 18:07
  • 1
    Please post your enum and the value of line[0]. The valueOf function expects an EXACT string. No extra spaces, punctuation, etc. I don't know if it's case sensitive but I'd assume it for now. – Mohamed Nuur May 19 '11 at 18:07
  • Can you add your Enum class and the content of the line array as well? Just to make sure we can see what are the inputs and what Enums you have. – TS- May 19 '11 at 18:08

2 Answers2

19

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
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I made sure of the case and they are in lower case but the trim is what I needed to do. Thank you. – Ryan Sullivan May 19 '11 at 18:17
  • 1
    @ajoobe you are calling `valueOf()` on an enum type. But I do not see this method in the [javadocs](http://docs.oracle.com/javase/6/docs/api/java/lang/Enum.html). Why so? The method declaration looks like `public static > T valueOf(Class enumType, String name)`. Here you are only passing the second parameter? What am I missing here? – Geek Mar 16 '13 at 07:49
  • 1
    @Geek, this is explained over [here](http://stackoverflow.com/questions/9803917/enum-valueofstring-name-missing-from-javadoc-1-5-and-1-6). – aioobe Mar 21 '13 at 08:06
-1

I am using a similar concept, but having a default value in case of fail

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;
        }
    }
}
Mircea Stanciu
  • 3,675
  • 3
  • 34
  • 37