Java XML Error Javax.xml.transform.TransformerException:java.lang.NullPointerException_java

Source: Internet
Author: User

Error javax.xml.transform.TransformerException:java.lang.NullPointerException solution for Java xml:

Using Java to manipulate XML, in the process of XML operation, to the last step, in the use of transformer XML conversion occurs when the NullPointerException error, some of the code is as follows:

 Convert 
transformerfactory tfactory =transformerfactory.newinstance (); 
Transformer Transformer = Tfactory.newtransformer (); 
An in-memory XML source file that needs to be converted 
Domsource Source = new Domsource (XMLDOC1); 
The generated XML file 
xmlDoc2 = new file (outputpath+generatexmlfilename); 
Streamresult result = new Streamresult (XMLDOC2); 
Convert 
transformer.transform (source, result); 

The following error occurred while running to the transform function (relatively long, because I am using swing to design the graphical interface, there will be some graphical interface event calls error):

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) at javax.swing.abstractbutton$ Handler.actionperformed (Abstractbutton.jav a:2318) at javax.swing.DefaultButtonModel.fireActionPerformed ( Defaultbuttonmodel. java:387) at the 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.processEven T (component.java:5806) at the Java.awt.Container.processEvent (container.java:2058) at java.awt.Component.dispatchEvent Impl (component.java:4413) at Java.awt.Container.dispatchEventImpl (container.java:2116) at Java.awt.Component.dispa Tchevent (component.java:4243) at Java.awt.LightweightDispatcher.retargetMouseEvent (container.java:4322) at JAVA.A Wt. 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.dispatchEvent Impl (window.java:2440) at Java.awt.Component.dispatchEvent (component.java:4243) at Java.awt.EventQueue.dispatchEve NT (eventqueue.java:599) at Java.awt.EventDispatchThread.pumpOneEventForFilters (Eventdispatchthre ad.java:273) at J Ava.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.aw T.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
:) 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.ap Ache.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) ... ---------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.ap Ache.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) at javax.swing.abstractbutton$ Handler.actionperformed (Abstractbutton.jav a:2318) at javax.swing.DefaultButtonModel.fireActionPerformed ( Defaultbuttonmodel. java:387) at the 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 (C ontainer.java:2058) at Java.awt.Component.dispatchEventImpl (component.java:4413) at Java.awt.Container.dispatchEventImpl (container.java:2116) at Java.awt.Component.dispatchEv ENT (component.java:4243) at Java.awt.LightweightDispatcher.retargetMouseEvent (container.java:4322) at Java.awt.Li Ghtweightdispatcher.processmouseevent (container.java:3986) at Java.awt.LightweightDispatcher.dispatchEvent ( container.java:3916) at Java.awt.Container.dispatchEventImpl (container.java:2102) at java.awt.Window.dispatchEvent Impl (window.java:2440) at Java.awt.Component.dispatchEvent (component.java:4243) at Java.awt.EventQueue.dispatchEve NT (eventqueue.java:599) at Java.awt.EventDispatchThread.pumpOneEventForFilters (Eventdispatchthre ad.java:273) at J Ava.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)
 

At first I was surprised that the transform function simply converted the XML tree in memory into a file, why there was a nullpointer error. Search the Internet, no one found the answer, finally in (http://blog.awe.cz/post/english/beware-of-null-text-nodes/) this blog to find quite useful answers.

In fact, take a serious look at the error information, you will find that there is a more important information:

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)

It can be seen from here that the characters function in the Com.sun.org.apache.xml.internal.serializer.ToUnknownStream.java file has an error, the number of rows is 317, to the JDK installation directory, Find SRC, find the Tounknownstream.java in the appropriate folder serializer, and locate the following function

/** 
  * 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);  
  }  

Obviously, note int length = Chars.length (); If the parameter chars is null, the nullpoiterexception error occurs when the length function is called. Again, the parse function (240 lines) of the Dom2to.java file under Com.sun.org.apache.xalan.internal.xsltc.trax:

Case Node.text_node: 
    _handler.characters (Node.getnodevalue ()); 
    Break 

Here you can see why the characters function has null arguments. The reason is that this node is a text node Text_node, and there is no content (NULL) in the call to this node, which returns NULL when Getnodevalue is invoked.

Up to the transform function, the node content is null, and in the Domsource class, because it is an XML tree built in memory, it must be null for one or more elements or nodes in this XML tree.

OK, the cause of the problem is found. The solution is simple: the Domsource element or node is NULL, it must be in the operation of XML, or modify the content, add elements and so on, so that the content to null. So what you need to do now is find the code that modifies or adds the content of the element, processes the possibility of NULL, and if it detects null, does not modify the XML or add the element. The simplest method is to use the IF statement. Before setNodeValue (string str) or settextcontent (string str), see if the parameter str is null (if (str = null), and not call this function if it is empty.

And in my program, it's true that I'm adding empty content to the element. The code is as follows: Deviceandidmap is a hashmap that returns NULL when the primary key does not exist when it calls get.

String neuronidstr = Deviceandidmap.get (namecontent); 
Update 
Neuronidelem.getfirstchild (). setNodeValue (NEURONIDSTR); 

So I add the IF statement to solve the problem, the code (this code is in the For loop, so skip this change with Coninue) as follows:

String neuronidstr = Deviceandidmap.get (namecontent); 
if (neuronidstr = = null) 
{ 
   continue 
} 
Update 
Neuronidelem.getfirstchild (). setNodeValue (NEURONIDSTR); 
               

Problem solved!

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.