jaxb
  1. jaxb
  2. JAXB-615

JAXBContext.newInstance fails with NPE if class has an Enum

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.1.8
    • Fix Version/s: not determined
    • Component/s: runtime
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Issuezilla Id:
      615

      Description

      if a class to be bound has a Enum then jaxb seems to assume that it is an enum
      (ie a specific type) and fails to bind. The exception produced is not helpful
      to diagnose the problem. ALso occurs when using generics and the erased type is
      Enum (this is how I found it)

      This was checked against 2.1.10 and 2.1.8 (2.1.10 in not in the bug version
      list)

      stack trace from 2.1.10 for the test code is

      Exception in thread "main" java.lang.NullPointerException
      at java.util.EnumMap.<init>(EnumMap.java:110)
      at com.sun.xml.bind.v2.model.impl.RuntimeEnumLeafInfoImpl.<init>
      (RuntimeEnumLeafInfoImpl.java:83)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createEnumLeafInfo
      (RuntimeModelBuilder.java:105)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createEnumLeafInfo
      (RuntimeModelBuilder.java:81)
      at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo
      (ModelBuilder.java:224)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo
      (RuntimeModelBuilder.java:100)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo
      (RuntimeModelBuilder.java:81)
      at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo
      (ModelBuilder.java:209)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo
      (RuntimeModelBuilder.java:95)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo
      (RuntimeModelBuilder.java:81)
      at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo
      (ModelBuilder.java:315)
      at com.sun.xml.bind.v2.model.impl.TypeRefImpl.calcRef
      (TypeRefImpl.java:92)
      at com.sun.xml.bind.v2.model.impl.TypeRefImpl.getTarget
      (TypeRefImpl.java:69)
      at com.sun.xml.bind.v2.model.impl.RuntimeTypeRefImpl.getTarget
      (RuntimeTypeRefImpl.java:58)
      at com.sun.xml.bind.v2.model.impl.RuntimeTypeRefImpl.getTarget
      (RuntimeTypeRefImpl.java:51)
      at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get
      (ElementPropertyInfoImpl.java:74)
      at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get
      (ElementPropertyInfoImpl.java:77)
      at java.util.AbstractList$Itr.next(AbstractList.java:422)
      at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo
      (ModelBuilder.java:255)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo
      (RuntimeModelBuilder.java:100)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo
      (RuntimeModelBuilder.java:81)
      at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo
      (ModelBuilder.java:209)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo
      (RuntimeModelBuilder.java:95)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo
      (RuntimeModelBuilder.java:81)
      at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo
      (ModelBuilder.java:315)
      at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo
      (ModelBuilder.java:330)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet
      (JAXBContextImpl.java:441)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>
      (JAXBContextImpl.java:288)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build
      (JAXBContextImpl.java:1111)
      at com.sun.xml.bind.v2.ContextFactory.createContext
      (ContextFactory.java:154)
      at com.sun.xml.bind.v2.ContextFactory.createContext
      (ContextFactory.java:121)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke
      (NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke
      (DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:363)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
      at jaxb.Issue.main(Issue.java:22)

        Activity

        Hide
        nddipiazza added a comment -

        I fixed it by changing any Class that contains an Enum Like the following:

        Change this:

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "CategoryInputType", propOrder =

        { "category" })
        public class CategoryInputType {

        @XmlElement(required = true)
        protected CategoryType category;

        /**

        •Gets the value of the category property.

        •@return
        •possible object is
        •{@link CategoryType }
        *
        */
        public CategoryType getCategory() { return category; }

        /**
        * Sets the value of the category property.
        *
        * @param value
        * allowed object is
        * {@link CategoryType }
        •*/
        public void setCategory(CategoryType value) { this.category = value; }

        }

        To this:


        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "CategoryInputType", propOrder = { "category" }

        )
        public class CategoryInputType {

        @XmlElement(required = true)
        protected CategoryType category;

        /**

        •Gets the value of the category property.

        •@return
        •possible object is

        {@link CategoryType }
        *
        */
        public Enum<CategoryType> getCategory() { return category; }

        /**
        * Sets the value of the category property.
        *
        * @param value
        * allowed object is
        * {@link CategoryType }

        •*/
        public void setCategory(Enum<CategoryType> value)

        { this.category = (CategoryType)value; }

        }

        Show
        nddipiazza added a comment - I fixed it by changing any Class that contains an Enum Like the following: Change this: @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "CategoryInputType", propOrder = { "category" }) public class CategoryInputType { @XmlElement(required = true) protected CategoryType category; /** •Gets the value of the category property. • •@return •possible object is •{@link CategoryType } * */ public CategoryType getCategory() { return category; } /** * Sets the value of the category property. * * @param value * allowed object is * {@link CategoryType } •*/ public void setCategory(CategoryType value) { this.category = value; } } To this: @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "CategoryInputType", propOrder = { "category" } ) public class CategoryInputType { @XmlElement(required = true) protected CategoryType category; /** •Gets the value of the category property. • •@return •possible object is • {@link CategoryType } * */ public Enum<CategoryType> getCategory() { return category; } /** * Sets the value of the category property. * * @param value * allowed object is * {@link CategoryType } •*/ public void setCategory(Enum<CategoryType> value) { this.category = (CategoryType)value; } }
        Hide
        nddipiazza added a comment -

        Came from:

        <simpleType name="CategoryType">
        <restriction base="string">
        <enumeration value="None" />
        <enumeration value="Component" />
        </restriction>
        </simpleType>

        Show
        nddipiazza added a comment - Came from: <simpleType name="CategoryType"> <restriction base="string"> <enumeration value="None" /> <enumeration value="Component" /> </restriction> </simpleType>
        Hide
        nddipiazza added a comment -

        I'm experiencing this issue when generating Java from WSDL using the CXF and WSDL2JAVA stuff.

        Is there a work around for this?

        Here is code generated that causes the problem:

        @XmlType(name = "CategoryType")
        @XmlEnum
        public enum CategoryType {

        @XmlEnumValue("None")
        NONE("None"),
        @XmlEnumValue("Component")
        COMPONENT("Component");
        private final String value;

        CategoryType(String v)

        { value = v; }

        public String value()

        { return value; }

        public static CategoryType fromValue(String v) {
        for (CategoryType c: CategoryType.values()) {
        if (c.value.equals(v))

        { return c; }

        }
        throw new IllegalArgumentException(v);
        }

        }

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "CategoryInputType", propOrder =

        { "category" }

        )
        public class CategoryInputType {

        @XmlElement(required = true)
        protected CategoryType category;

        /**

        • Gets the value of the category property.
        • @return
        • possible object is
        • {@link CategoryType }
          *
          */
          public CategoryType getCategory() { return category; }

          /**
          * Sets the value of the category property.
          *
          * @param value
          * allowed object is
          * {@link CategoryType }
        • */
          public void setCategory(CategoryType value)

          { this.category = value; }

        }

        Show
        nddipiazza added a comment - I'm experiencing this issue when generating Java from WSDL using the CXF and WSDL2JAVA stuff. Is there a work around for this? Here is code generated that causes the problem: @XmlType(name = "CategoryType") @XmlEnum public enum CategoryType { @XmlEnumValue("None") NONE("None"), @XmlEnumValue("Component") COMPONENT("Component"); private final String value; CategoryType(String v) { value = v; } public String value() { return value; } public static CategoryType fromValue(String v) { for (CategoryType c: CategoryType.values()) { if (c.value.equals(v)) { return c; } } throw new IllegalArgumentException(v); } } @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "CategoryInputType", propOrder = { "category" } ) public class CategoryInputType { @XmlElement(required = true) protected CategoryType category; /** Gets the value of the category property. @return possible object is {@link CategoryType } * */ public CategoryType getCategory() { return category; } /** * Sets the value of the category property. * * @param value * allowed object is * {@link CategoryType } */ public void setCategory(CategoryType value) { this.category = value; } }
        Hide
        Martin Grebac added a comment -

        Thanks for submission. Reassigning to myself, adjusting priority.

        Show
        Martin Grebac added a comment - Thanks for submission. Reassigning to myself, adjusting priority.
        Hide
        skells added a comment -

        Created an attachment (id=300)
        code to demonstrate the problem

        Show
        skells added a comment - Created an attachment (id=300) code to demonstrate the problem

          People

          • Assignee:
            Iaroslav Savytskyi
            Reporter:
            skells
          • Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated: