Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException
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);
问题解决!
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: asp.net cookie的读写实例
推荐阅读
-
Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException
-
解决出现 java.lang.ExceptionInInitializerError错误问题
-
java eclipse 出现 xxx cannot be resolved to a type 错误解决方法
-
java eclipse 出现 xxx cannot be resolved to a type 错误解决方法
-
Eclipse 开发java 出现Failed to create the Java Virtual Machine错误解决办法
-
Eclipse 开发java 出现Failed to create the Java Virtual Machine错误解决办法
-
Java使用正则表达式截取重复出现的XML字符串功能示例
-
Java使用正则表达式截取重复出现的XML字符串功能示例
-
java session出现的错误
-
AspectJ 出现错误::0 can't find referenced pointcut 的解决之道 博客分类: AspectJ aspectj切面java架构开发