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.transform(TransformerImpl.java:717) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313) at operation.AddNeuronID.addNeuronId(AddNeuronID.java:273) at operationUI.AddNeuronIDListener.constructor2Perform(AddNeuronIDListener.java:245) at operationUI.AddNeuronIDListener.actionPerformed(AddNeuronIDListener.java:85) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.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(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.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.transformIdentity(TransformerImpl.java:662) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(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.transformIdentity(TransformerImpl.java:662) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313) at operation.AddNeuronID.addNeuronId(AddNeuronID.java:273) at operationUI.AddNeuronIDListener.constructor2Perform(AddNeuronIDListener.java:245) at operationUI.AddNeuronIDListener.actionPerformed(AddNeuronIDListener.java:85) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.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(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.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.NullPointerExceptionat 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);
問題解決!
感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!