JAXBContext.newInstance(contextPath)期间出现NullPointerException - jakarta.xml.bind

问题描述 投票:0回答:1

在我的基于 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" 

或者无论如何,我总是得到空指针。

在我的 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 文档 但相反,我总是得到一个空指针。

你能告诉我这个空指针的原因是什么吗?

java jakarta-ee nullpointerexception jaxb xml-binding
1个回答
0
投票

异常不是由于 JAXBContext.newInstance(contextPath) 期间的 NullPointer 引起的,而是由于缺少依赖库 org.glassfish.jaxb.runtime 导致的 NoClassDefFound 错误。当我将其添加到 pom.xml 时,问题得到了解决。

© www.soinside.com 2019 - 2024. All rights reserved.