Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException解决办法:
利用Java操作XML,在操作XML过程中,执行到最后一步,在利用Transformer进行XML转换时出现NullPointerException错误,出问题的部分代码如下:
//转换 TransformerFactory tFactory =TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); //需要转换的内存中XML源文件 DOMSource source = new DOMSource(xmlDoc1); //生成的xml文件 File xmlDoc2 = new File(outPutPath+generateXmlFileName); StreamResult result = new StreamResult(xmlDoc2); //转换 transformer.transform(source, result);
运行到transform函数时出现以下错误(比较长,由于我是利用SWING设计图形界面的,所以会有一些图形界面事件调用的错误):
javax.xml.transform.TransformerException: java.lang.NullPointerException at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transfor m(TransformerImpl.java:717) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transfor m(TransformerImpl.java:313) at operation.AddNeuronID.addNeuronId(AddNeuronID.java:273) at operationUI.AddNeuronIDListener.constructor2Perform(AddNeuronIDListen er.java:245) at operationUI.AddNeuronIDListener.actionPerformed(AddNeuronIDListener.j ava:85) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19 95) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav a:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel .java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242 ) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL istener.java:236) at java.awt.Component.processMouseEvent(Component.java:6041) at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) at java.awt.Component.processEvent(Component.java:5806) at java.awt.Container.processEvent(Container.java:2058) at java.awt.Component.dispatchEventImpl(Component.java:4413) at java.awt.Container.dispatchEventImpl(Container.java:2116) at java.awt.Component.dispatchEvent(Component.java:4243) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322 ) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) at java.awt.Container.dispatchEventImpl(Container.java:2102) at java.awt.Window.dispatchEventImpl(Window.java:2440) at java.awt.Component.dispatchEvent(Component.java:4243) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre ad.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) Caused by: java.lang.NullPointerException at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters (ToUnknownStream.java:317) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :240) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :132) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :94) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transfor mIdentity(TransformerImpl.java:662) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transfor m(TransformerImpl.java:708) ... 29 more --------- java.lang.NullPointerException at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters (ToUnknownStream.java:317) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :240) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :226) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :132) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java :94) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transfor mIdentity(TransformerImpl.java:662) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transfor m(TransformerImpl.java:708) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transfor m(TransformerImpl.java:313) at operation.AddNeuronID.addNeuronId(AddNeuronID.java:273) at operationUI.AddNeuronIDListener.constructor2Perform(AddNeuronIDListen er.java:245) at operationUI.AddNeuronIDListener.actionPerformed(AddNeuronIDListener.j ava:85) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19 95) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav a:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel .java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242 ) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL istener.java:236) at java.awt.Component.processMouseEvent(Component.java:6041) at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) at java.awt.Component.processEvent(Component.java:5806) at java.awt.Container.processEvent(Container.java:2058) at java.awt.Component.dispatchEventImpl(Component.java:4413) at java.awt.Container.dispatchEventImpl(Container.java:2116) at java.awt.Component.dispatchEvent(Component.java:4243) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322 ) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) at java.awt.Container.dispatchEventImpl(Container.java:2102) at java.awt.Window.dispatchEventImpl(Window.java:2440) at java.awt.Component.dispatchEvent(Component.java:4243) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre ad.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
一开始我也感到奇怪,transform函数只是把在内存中的XML树转换成文件,为何会出现nullPointer错误呢。上网查了一下,没什么人找到答案,最后在(http://blog.awe.cz/post/english/beware-of-null-text-nodes/)这个blog中找到相当有用的答案.
其实认真看看出错的信息,会发现有个信息比较重要:
java.lang.NullPointerException at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(ToUnknownStream.java:317) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:240)
从这里可以看出是由于com.sun.org.apache.xml.internal.serializer.ToUnknownStream.java文件中的characters函数出错,所在行数是317,到jdk安装目录,找到src,到相应的文件夹serializer中找到ToUnknownStream.java,找到以下函数
/** * Converts the String to a character array and calls the SAX method * characters(char[],int,int); * * @see ExtendedContentHandler#characters(String) */ public void characters(String chars) throws SAXException { final int length = chars.length(); if (length > m_charsBuff.length) { m_charsBuff = new char[length*2 + 1]; } chars.getChars(0, length, m_charsBuff, 0); this.characters(m_charsBuff, 0, length); }
很明显,注意int length = chars.length(); 如果参数chars是null的话,调用length函数就会出现nullpoiterException错误。再上一层,com.sun.org.apache.xalan.internal.xsltc.trax下的DOM2TO.java文件的parse函数(240行):
case Node.TEXT_NODE: _handler.characters(node.getNodeValue()); break;
在这里,就可以知道characters函数为什么出现null参数了。原因是此结点是文本结点TEXT_NODE,而调用本结点中没有内容(null),当调用getNodeValue时,返回null.
上升到transform函数,出现结点内容为null,那在DOMSource类中,由于它是在内存中建立起来的XML树,所以肯定是这个XML树中的有一个或多个元素或结点的内容为null。
OK,问题原因找到。解决办法很简单,DOMSource中有元素或结点为null,那一定是在操作XML时,或者是修改内容,添加元素等等操作,使内容变为null。所以现在需要做的就是找到修改或添加元素内容的代码,把有可能出现null的情况进行处理,如果检测到为null,则不修改XML或不添加此元素。最简单的方法就是用if语句。即setNodeValue(String str)或setTextContent(String str)之前,先查看参数str是否为空(if(str == null)),如果空则不调用此函数。
而在我的程序中,确实我是把空的内容加入到元素中。代码如下:deviceAndIDMap是一个HashMap,当它调用get时,不存在此主键时,会返回null.
String neuronIdStr = deviceAndIDMap.get(nameContent); //更新 neuronIdElem.getFirstChild().setNodeValue(neuronIdStr);
因此我加一下if语句,就可以解决问题了,代码(此代码是在for循环中的,因此用coninue来跳过本次的修改)如下:
String neuronIdStr = deviceAndIDMap.get(nameContent); if(neuronIdStr == null) { continue; } //更新 neuronIdElem.getFirstChild().setNodeValue(neuronIdStr);
问题解决!
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
我试图使用javamail示例包中的ShowMsg.java读取gmail消息,但在运行时不断出现此错误。虽然程序编译得很好。 以下是错误消息: 我运行的命令是,编译它的命令是。javax.mail.jar包含在lib文件夹中 这是一些更短的替代代码,并得到相同的错误:
问题内容: 当前在WTForms中访问错误,您必须像这样遍历字段错误: 由于我正在构建一个不使用任何表单视图的rest应用程序,因此我不得不检查所有表单字段以查找错误所在。 有没有办法可以做类似的事情: 问题答案: 实际对象的属性包含字典中的字段名称及其错误。因此,您可以执行以下操作:
我正在尝试一个非常基本的规则来测试流口水是否有效,我将其与Hibernate连接,但我发现错误“java.lang.ArrayIndexOutOfBoundsException:-1”这是main:包metier; 以及。drl文件为: 出现的错误是: 你知道它是从哪里来的吗?我要怎么修理它?我真的很感激
我编译并使用mod for MC 1.7。10在想法上没有问题。将我的mod放入项目后,AltLauncher中的“暗物质”会显示错误 在块类中,我写这个构造函数 我认为这门课是
我正在尝试使用MCP6.2和Eclipse为Minecraft制作一个GUI模块。GUI(到目前为止)由和一个纹理矩形组成。问题是,每当我尝试mod时,纹理矩形都是白色的,并且说。我尝试重定向到一个默认的纹理,比如/gui/firen。而且工作正常。任何新添加的纹理都会带来这个问题,即使我用新名称复制现有纹理。 我要显示的纹理位于MCP文件的以下目录中: mcp\jars\minecraft。ja
登录后,它生成了一个哈希值,但仍然给出错误“Some problem currened!try tain”。
我有问题:Win 8.1(新鲜,刚刚安装),火狐34(降级,也在38和39测试),硒2.46.0...我收到: “OpenQA”类型的未处理异常。硒。WebDriverException“”在WebDriver中发生。动态链接库 其他信息:未能在45000毫秒内启动套接字。尝试连接到以下地址:127.0.0.1:7055 你知道如何让它运行吗? 在Win7的虚拟服务器上-相同的项目运行良好...
我试图查看Eclipse(版本Mars.2)中提供的springboot初学者项目,但遇到了错误。 这就是我所做的: 选择spring boot会给我两个新项目: ` 谷歌搜索此错误导致我进入maven存储库,所以我导入ch.qos.logback 再次运行(运行作为…- ``` 谢谢你的帮助!