11

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.

3
  • 2
    and what does your enum look like? Commented May 19, 2011 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. Commented May 19, 2011 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. Commented May 19, 2011 at 18:08

2 Answers 2

21

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
Sign up to request clarification or add additional context in comments.

3 Comments

I made sure of the case and they are in lower case but the trim is what I needed to do. Thank you.
@ajoobe you are calling valueOf() on an enum type. But I do not see this method in the javadocs. Why so? The method declaration looks like public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name). Here you are only passing the second parameter? What am I missing here?
@Geek, this is explained over here.
-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;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.