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?

Sash_KP
  • 5,551
  • 2
  • 25
  • 34
  • 1
    seeing as we have no idea which part causes the NPE, how do you want us to respond? – Stultuske Mar 09 '22 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. – Sash_KP Mar 09 '22 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. – Stultuske Mar 09 '22 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 https://stackoverflow.com/questions/4566672/nullpointerexception-in-jaxbcontext-newinstance ? 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. – Sash_KP Mar 09 '22 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 – Stultuske Mar 09 '22 at 17:13
  • The moment i call this even in a System.out.print("xyz: "+JAXBContext.newInstance(xontextPath)) , i hit nullpointer. All logs before that line gets printed and the next line from this above sop line doesn't get printed. – Sash_KP Mar 09 '22 at 17:24
  • and your point is? Have you checked the stacktrace? – Stultuske Mar 10 '22 at 07:16
  • You're correct. When i put that statement in try catch block, i found that NoClassDefFound error was seen as it was expecting org.glassfish.jaxb:jaxb-runtime but wasn't finding. When i added it in pom.xml , the classdefnotfound error is gone now. However am seeing the below error now and am trying to figure out the below issue now. – Sash_KP Mar 10 '22 at 07:32
  • Two classes have the same XML type name "objectFactory". Use XmlType.name and XmlType.namespace to assign different names to them. this problem is related to the following location: at com.abc.model.ObjectFactory this problem is related to the following location: at com.bcd.core.ObjectFactory ........ org.glassfish.jaxb.runtime.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:76) at org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:419) – Sash_KP Mar 10 '22 at 07:33

1 Answers1

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.

Sash_KP
  • 5,551
  • 2
  • 25
  • 34