Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: No enum const class

Tags:

java

enums

jaxb

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

like image 565
Will Sumekar Avatar asked Jul 18 '11 07:07

Will Sumekar


3 Answers

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

like image 66
Kai Avatar answered Sep 25 '22 00:09

Kai


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.

like image 25
everton Avatar answered Sep 25 '22 00:09

everton


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.

like image 25
Talha Ahmed Khan Avatar answered Sep 27 '22 00:09

Talha Ahmed Khan