JAXBContext.newInstance(contextPath) 期间出现 NullPointerException - jakarta.xml.bind
在我的基于 Maven 的项目中,字符串“contextPath”只不过是我项目的任何包。无论我分配给 contextPath 的是哪个包(或附加有 :) 的多个包,我仍然会得到 NullPointerException。
所以我在 JAXBContext.newInstance(contextPath) 期间得到 NullPointerException。
import jakarta.xml.bind.JAXBContext;
...
private String contextPath = "com.abc.core";
...
JAXBContext jc = JAXBContext.newInstance(contextPath);
即使 contextPath 是
"com.abc.core:com.bcd:core"
Or ,我总是得到空指针。
在我的 pom.xml 文件中:
<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>
我至少期待 JAXBException,但总是得到 NullPointerException。请注意,我使用的是 jakarta.xml.bind 而不是 javax.xml.bind。并使用 jdk 8。
我尝试将 jakarta.xml.bind 升级到 3.0.1 ,但仍然看到同样的问题。尝试检查 mvn dependency:tree 以检查其他人是否强制使用 jakarta.xml.bind 的其他版本,但没有从那里得到任何线索。
我检查了 jakarta.xml.bind 的文档中的 JAXBContext,但根据文档,如果 contextPath 不正确,我应该得到 JAXBException。 - JAXBContext 文档 但相反,我总是得到一个空指针。
您能告诉我这个 NullPointer 的原因是什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该异常不是由于 JAXBContext.newInstance(contextPath) 期间的 NullPointer 引起的,而是由于缺少依赖库 org.glassfish.jaxb.runtime 而导致的 NoClassDefFound 错误。当我将其添加到 pom.xml 时,问题得到了解决。
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.