prime
prime

Reputation: 15564

Encountering DescriptorException [EclipseLink-108] During JAXB Marshalling of a Java Class

I am currently engaged in a project involving a substantial number of Java classes, which I am responsible for migrating to Jakarta EE. These Java classes were generated from an .xsd file, and I possess limited control over their configuration and structure. (It's a separate Library)

I have created a sample project to recreate the issue in a smaller project and the classes are like below.

All imports are from jakarta.xml.bind.annotation

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"name", "age"})
@XmlSeeAlso({Bird.class})
public class Animal {
    protected String name;
    protected int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"description"})
@XmlSeeAlso({Parrot.class})
public class Bird extends Animal {
    protected String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "parrot", propOrder = {"color"})
public class Parrot extends Bird {
    protected String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

The pom.xml has following dependencies,

<properties>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>4.0.2</version>
        </dependency>
    </dependencies>

The error is getting thrown when I try to marshal a Bird object to xml.

A sample code for that is as below,

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;

public static void ObjectToXML() {
        Bird bird = new Bird();
        bird.setDescription("can fly");
        bird.setName("parrot");
        bird.setAge(10);

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Bird.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            System.out.println("marshaller : " + marshaller.getClass());
            // In a working scenario, this is getting printed as `marshaller : class org.eclipse.persistence.jaxb.JAXBMarshaller`

            StringWriter sw = new StringWriter();
            marshaller.marshal(bird, sw);
            System.out.println(sw.toString());
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

The error message is as follows,

jakarta.xml.bind.JAXBException: 
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-108] (Eclipse Persistence Services - 4.0.2.v202306161219): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Cannot find value in class indicator mapping in parent descriptor [XMLDescriptor(animal.Bird --> [])].
Descriptor: XMLDescriptor(animal.Parrot --> [])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException: Cannot invoke "String.intern()" because "xPath" is null

 - with linked exception:
[Exception [EclipseLink-0] (Eclipse Persistence Services - 4.0.2.v202306161219): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-108] (Eclipse Persistence Services - 4.0.2.v202306161219): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Cannot find value in class indicator mapping in parent descriptor [XMLDescriptor(animal.Bird --> [])].
Descriptor: XMLDescriptor(animal.Parrot --> [])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException: Cannot invoke "String.intern()" because "xPath" is null
]
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1173)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:208)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:167)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:154)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:114)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:104)
    at org.eclipse.persistence.jaxb.XMLBindingContextFactory.createContext(XMLBindingContextFactory.java:43)
    at jakarta.xml.bind.ContextFinder.find(ContextFinder.java:373)
    at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:605)
    at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:546)
    at animal.ErrorDemo.ObjectToXML(ErrorDemo.java:22)
    at animal.ErrorDemo.main(ErrorDemo.java:12)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 4.0.2.v202306161219): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-108] (Eclipse Persistence Services - 4.0.2.v202306161219): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Cannot find value in class indicator mapping in parent descriptor [XMLDescriptor(animal.Bird --> [])].
Descriptor: XMLDescriptor(animal.Parrot --> [])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException: Cannot invoke "String.intern()" because "xPath" is null

    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:762)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:698)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:629)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:875)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:825)
    at org.eclipse.persistence.oxm.XMLContext$XMLContextState.setupSession(XMLContext.java:763)
    at org.eclipse.persistence.oxm.XMLContext$XMLContextState.setupSession(XMLContext.java:671)
    at org.eclipse.persistence.internal.oxm.Context$ContextState.<init>(Context.java:82)
    at org.eclipse.persistence.oxm.XMLContext$XMLContextState.<init>(XMLContext.java:726)
    at org.eclipse.persistence.oxm.XMLContext.<init>(XMLContext.java:198)
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1197)
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1170)
    ... 11 more

As I narrowed it down I can get rid of this error if I simply change name = "parrot" to name = "" in the Parrot class. Or change name = "" to name = "bird" in Bird class or remove @XmlSeeAlso({Parrot.class}) in the Bird class.

But all of these options are not directly possible as I have limited control over these files in the actual project (These classes are coming from a separate library - I'd like to touch that library if there is nothing else can be done to fix it but to change these classes)

Is this a problem with the Java classes or the libraries I'm using? Any help to fix this would be really appreciated.

Upvotes: 0

Views: 106

Answers (0)

Related Questions