我正在尝试解析XML并获得一个我见过的最奇怪的NullPointerException
。这是堆栈跟踪:
java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:399)
at weblogic.xml.jaxp.DOMFactoryProperties.<init>(DOMFactoryProperties.java:44)
at weblogic.xml.jaxp.DOMFactoryProperties.clone(DOMFactoryProperties.java:114)
at weblogic.xml.jaxp.RegistryDocumentBuilderFactory.newDocumentBuilder(RegistryDocumentBuilderFactory.java:140)
at com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.createDocument(SAX2DOM.java:324)
at com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.<init>(SAX2DOM.java:84)
at com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory.getSerializationHandler(TransletOutputHandlerFactory.java:187)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(TransformerImpl.java:392)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerHandlerImpl.setResult(TransformerHandlerImpl.java:137)
at com.sun.xml.bind.v2.runtime.unmarshaller.DomLoader$State.<init>(DomLoader.java:74)
at com.sun.xml.bind.v2.runtime.unmarshaller.DomLoader.startElement(DomLoader.java:113)
at com.sun.xml.bind.v2.runtime.unmarshaller.ProxyLoader.startElement(ProxyLoader.java:55)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:455)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:433)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2755)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at weblogic.xml.jaxp.WebLogicXMLReader.parse(WebLogicXMLReader.java:133)
at weblogic.xml.jaxp.RegistryXMLReader.parse(RegistryXMLReader.java:173)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:105)
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:584)
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:571)
首先,它只发生在我的应用程序部署在Tomcat下时(我使用weblogic客户端JAR连接到外部weblogic JMS队列)。我无法在Tomcat之外的测试中重现它。所以我怀疑一些类加载器的怪异或什么。
其次,我尝试反编译DOMFactoryProperties
类,这是导致该NPE的构造函数:
public class DOMFactoryProperties
{
public static String COALESCING = "Coalescing";
public static String EXPANDENTITYREFERENCES = "ExpandEntityReferences";
public static String IGNORINGCOMMENTS = "IgnoringComments";
public static String IGNORINGELEMENTCONTENTWHITESPACE = "IgnoringElementContentWhitespace";
public static String NAMESPACEAWARE = "Namespaceaware";
public static String VALIDATING = "Validating";
public static String SCHEMA = "Schema";
public static String XINCL = "XIncludeAware";
private Hashtable factoryProperties;
private Set facPropertySettingMarks;
private LinkedHashMap attributes;
public DOMFactoryProperties()
{
this.factoryProperties = new Hashtable();
this.factoryProperties.put(COALESCING, Boolean.FALSE);
this.factoryProperties.put(EXPANDENTITYREFERENCES, Boolean.TRUE);
this.factoryProperties.put(IGNORINGCOMMENTS, Boolean.FALSE);
this.factoryProperties.put(IGNORINGELEMENTCONTENTWHITESPACE, Boolean.FALSE);
this.factoryProperties.put(NAMESPACEAWARE, Boolean.FALSE);
this.factoryProperties.put(VALIDATING, Boolean.FALSE);
this.factoryProperties.put(XINCL, Boolean.FALSE);
this.facPropertySettingMarks = new HashSet();
this.attributes = new LinkedHashMap();
}
//...
}
this.factoryProperties.put(COALESCING, Boolean.FALSE);
行失败了。造成这种情况的唯一原因可能是null
中的COALESCING
值,但除非其他一些类从外部设置为null
(由于某种原因public static
)不明白为什么它可以是null
。
有什么想法吗?
有没有办法告诉Spring的 Jaxb2Marshaller
不使用weblogic的XML阅读器?
更新:更糟糕的是,对于相同的XML,这在Tomcat下甚至不能总是可重现。有时候它会起作用,有时却不起作用。
UPDATE2 :调试显示weblogic.xml.jaxp.DOMFactoryProperties.COALESCING
确实是null
。但为什么呢?
UPDATE3 :我可以通过将SAXParserFactory
系统属性设置为javax.xml.parsers.SAXParserFactory
(平台默认值)来判断要使用com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
的哪个实现。另一个问题仍然存在。
答案 0 :(得分:1)
我们曾经有过类似的问题,也只发生在Tomcat上。正如我所看到的,您的应用程序使用apache xalan(请查看堆栈跟踪)。我们的深入调查证明它是由解析器工厂的不恰当实例化引起的(我记得) - 可能是库之间的一些奇怪的冲突。在我们的例子中,将xalan JAR添加到项目maven依赖项(它应该在您的webapp的lib
目录中可用)解决了这个问题。我的意思是 - 即使项目在没有它的情况下成功构建,我们也必须明确地添加这个依赖。
答案 1 :(得分:0)
非常感谢@Piotr De!
我有这个例外:
显示java.lang.NullPointerException at com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory.getSerializationHandler(TransletOutputHandlerFactory.java:186) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(TransformerImpl.java:453) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:336) 在com.xml.fda.sciencepapers.service.impl.DisplayServiceImpl.generateHTML(DisplayServiceImpl.java:114)
...
DisplayServiceImpl中的第114行调用transform
的{{1}}方法。
我通过将xalan依赖添加到我的pom中解决了这个问题:
javax.xml.transform.Transformer
然后得到一些Serializer类没有找到Exception,我也添加了这个依赖:
<!-- https://mvnrepository.com/artifact/xalan/xalan -->
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.2</version>
</dependency>
以下是下载serializer jar。
注意:我自己创建了lib文件夹,将其放在resources文件夹中并将jar放入其中。
编辑:通过上面的文字描边,因为我将地方从我从本地文件夹到maven存储库的地方改变了。