枚举valueOf IllegalArgumentException:没有枚举const类 [英] enum valueOf IllegalArgumentException: No enum const class

查看:65
本文介绍了枚举valueOf IllegalArgumentException:没有枚举const类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去在Java中使用过枚举,但是由于某种原因,我现在遇到一个奇怪的错误.它引发错误的代码行是:

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()){
    ...
}

我得到一个

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.

推荐答案

枚举常量区分大小写,因此请确保您的常量确实是小写字母.另外,我建议您也 trim()输入,以确保其中没有前导/尾随空白:

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;
        }
    }
}

输出:

Property 1

这篇关于枚举valueOf IllegalArgumentException:没有枚举const类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆