简体   繁体   中英

NullPointerException during JAXBContext.newInstance(contextPath) - jakarta.xml.bind

In my maven based project, String "contextPath" is nothing but any package of my project. No matter what package(or multiple packages appended with:) i assign to contextPath, i still get NullPointerException.

So i get NullPointerException during, JAXBContext.newInstance(contextPath).

import jakarta.xml.bind.JAXBContext;
...
private String contextPath = "com.abc.core"; 
...
JAXBContext jc = JAXBContext.newInstance(contextPath);

Even if that contextPath is

"com.abc.core:com.bcd:core" 

Or whatsoever, i get nullpointer always.

In my pom.xml file:

                        <dependency>
                                <groupId>jakarta.xml.bind</groupId>
                                <artifactId>jakarta.xml.bind-api</artifactId>
                                <version>3.0.1</version>
                        </dependency>
                        <dependency>
                                <groupId>com.sun.xml.bind</groupId>
                                <artifactId>jaxb-impl</artifactId>
                                <version>3.0.1</version>
                                <scope>runtime</scope>
                        </dependency>

I am expecting at least JAXBException, but getting NullPointerException always. Note that i am using jakarta.xml.bind and not javax.xml.bind. And using jdk 8.

I have tried upgrading jakarta.xml.bind to 3.0.1, but still seeing the same issue. Tried checking mvn dependency:tree to check if someone else is forcing some other version of jakarta.xml.bind to use, but didn't get any clue from there.

I checked the docs of jakarta.xml.bind for JAXBContext, but as per doc i should get JAXBException if contextPath is not proper. - docs for JAXBContext But instead, i always get a nullpointer.

Can you please let me know what might be the reason for this NullPointer?

The exception was not due to NullPointer during JAXBContext.newInstance(contextPath), but a NoClassDefFound error due to absence of a dependent lib org.glassfish.jaxb.runtime. When i added this to the pom.xml, the issue got resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM