java.lang.IllegalArgumentException:没有枚举常量类

发布于 2024-11-24 20:45:30 字数 1722 浏览 2 评论 0原文

我使用 JAXB 2 根据 XSD 模式解析 XML 文件,并且 XML 标记在 ant 构建为 Java 类的过程中自动解组。创建了一些枚举。代码是:

@XmlType(name = "binQuality")
@XmlEnum
public enum BinQuality {

    GOOD,
    BAD,
    UGLY,
    NULL;

    public String value() {
        return name();
    }

    public static BinQuality fromValue(String v) {
        return valueOf(v);
    }
}

在我的代码中,我在循环中调用:

BinQuality bq = BinQuality.valueOf(him.getToBinQuality());

,并且仅在第 91 次迭代中得到异常。

******* 已更新* ******

him.getToBinQuality() 确实返回一个有效的枚举(GOOD/BAD/丑陋/空)。以下是日志的摘录。

....
2011-07-18 15:28:09 DEBUG (com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl:183) -> class com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl|exportToXml|him.getToBin():89|him.getToBinQuality():BAD
2011-07-18 15:28:09 DEBUG (com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl:183) -> class com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl|exportToXml|him.getToBin():90|him.getToBinQuality():UGLY
2011-07-18 15:28:09 DEBUG (com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl:183) -> class com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl|exportToXml|him.getToBin():91|him.getToBinQuality():BAD 
2011-07-18 15:28:09 WARN  (org.apache.struts.action.RequestProcessor:538) -> Unhandled Exception thrown: class java.lang.IllegalArgumentException

这看起来确实很神秘。

使用的Java版本是1.5。

欣赏它。

将要

I use JAXB 2 to parse an XML file against an XSD schema and the XML tags are automatically unmarshalled during ant build to Java classes. Some enums are created. The code is:

@XmlType(name = "binQuality")
@XmlEnum
public enum BinQuality {

    GOOD,
    BAD,
    UGLY,
    NULL;

    public String value() {
        return name();
    }

    public static BinQuality fromValue(String v) {
        return valueOf(v);
    }
}

In my code I call:

BinQuality bq = BinQuality.valueOf(him.getToBinQuality());

in a loop and I get the exception only in the 91st iteration.

******* UPDATED *******

him.getToBinQuality() does return a valid enum (GOOD/BAD/UGLY/NULL). Below is an excerpt of logs.

....
2011-07-18 15:28:09 DEBUG (com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl:183) -> class com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl|exportToXml|him.getToBin():89|him.getToBinQuality():BAD
2011-07-18 15:28:09 DEBUG (com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl:183) -> class com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl|exportToXml|him.getToBin():90|him.getToBinQuality():UGLY
2011-07-18 15:28:09 DEBUG (com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl:183) -> class com.st.mas.wmr.persistence.process.ProcessStifOliBinConversionCompleteImpl|exportToXml|him.getToBin():91|him.getToBinQuality():BAD 
2011-07-18 15:28:09 WARN  (org.apache.struts.action.RequestProcessor:538) -> Unhandled Exception thrown: class java.lang.IllegalArgumentException

This seems really mysterious.

Java version used is 1.5.

Appreciate it.

Will

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心清如水 2024-12-01 20:45:30

这是因为找不到第 91 个条目的枚举值。失败的字符串的值是多少?

This is because no enum value could be found for your 91th entry. What is the value of the failed String?

冷…雨湿花 2024-12-01 20:45:30

您的 XML 没有有效的 ENUM 值(例如小写的“good”),或者它有一个空标签,因为如果您尝试通过 MyEnum.valueOf(null) 评估枚举元素,它将抛出 IllegalArgumentException。

Either your XML does not have a valid ENUM value (like 'good' in lowercase) or it has an empty tag, because if you try to eval an enum element via MyEnum.valueOf(null) it will throw an IllegalArgumentException.

淡淡的优雅 2024-12-01 20:45:30

这很可能是因为 him.getToBinQuality() 没有返回有效的字符串,在本例中 String 应该是 'GOOD|BAD|UGLY|NULL'

您可以通过在日志上打印该值可以轻松调试此问题。

Most probably this is because that him.getToBinQuality() does not return the valid string which is in this case String should be 'GOOD|BAD|UGLY|NULL'

You can easily debug this by printing the value on the log.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文