0

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?

9
  • 1
    seeing as we have no idea which part causes the NPE, how do you want us to respond? Commented Mar 9, 2022 at 16:49
  • As i have already mentioned, i have confirmed that the nullpointer is thrown right there when i call JAXBContext.newInstance(contextPath). I have imported jakarta.xml.bind.JAXBContext and contextPath here can be any package , no matter what package i give there, i get a nullpointer always. Commented Mar 9, 2022 at 16:53
  • so, you mean to say that a static method call is throwing the NPE? Seems unlikely, since JAXBContext can't be null. Commented Mar 9, 2022 at 17:05
  • Yeah correct, that is the strange part here. I have printed debug logs right before and after that line , and confirmed that this static method call itself is throwing NPE. For some reason JAXBContext is not loading. Can you please check stackoverflow.com/questions/4566672/… ? Here also the same thing was reported , but with very old version. And here the OP had mentioned that some other pom.xml was causing the issue. Commented Mar 9, 2022 at 17:11
  • if you had looked at that stacktrace there, you would understand that it's not that line itself that throws the NPE Commented Mar 9, 2022 at 17:13

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.