2012-09-25 40 views
-1

我有以下幾點:NullPointerException異常,而試圖解析XML文件

public static void main(String args[]) { 

    // upload config' data for program - param' are path and Xml's Root node/ where to get data from 
    confLoader conf = new confLoader("conf.xml", "config"); 

    System.out.println(conf.getDbElement("dataSource")); 
    System.out.println(conf.getDbElement("dataSource")); 
    System.out.println(conf.getDbElement("dataSource")); // Fails 
... 

這是負責建立DOM和( 'getDbElement()')解析代碼:

public class confLoader{ 

DocumentBuilderFactory docBuilderFactory; 
DocumentBuilder docBuilder; 
Document doc; 
NodeList nList; 

public confLoader(String path, String XmlRoot){ 

    try { 
      docBuilderFactory = DocumentBuilderFactory.newInstance(); 
      docBuilder = docBuilderFactory.newDocumentBuilder(); 
      doc = docBuilder.parse(new File(path)); 
      // normalize text representation 
      doc.getDocumentElement().normalize();     
    nList = doc.getElementsByTagName(XmlRoot); 

    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
} 

public String getDbElement(String element) { 

    Node nNode = nList.item(0); // 1st item/node - sql 
    try { 
     if (nNode.getNodeType() == Node.ELEMENT_NODE) {  ///// Line 36 - Problematic 

      Element eElement = (Element) nNode; 
      return (((Node) eElement.getElementsByTagName(element).item(0).getChildNodes().item(0)).getNodeValue()); 
     } 
    } catch (Exception ex) { 
     System.out.println("Error retrieving " + element + " :" + ex.getMessage());//Thread.dumpStack(); 
     ex.printStackTrace(); 
     } 
    return "not available"; 
} 

} 

堆棧跟蹤給定代碼:

jdbc:mysql://localhost:... 
    java.lang.NullPointerException 
    jdbc:mysql://localhost:... 
    Error retrieving dataSource :null 
    not available 
    at exercise.confLoader.getDbElement(confLoader.java:36) 
    at exercise.Exercise.main(Exercise.java:22) 

     Line 36 : if (nNode.getNodeType() == Node.ELEMENT_NODE) 

xml解析完成兩次,第三次嘗試從xml解析時,我得到NullPointerException。

+0

使用一些println()或者用調試器遍歷代碼來查看哪個對象爲null,然後可以計算出如何修復它。究竟是什麼情況發生?你有沒有堆棧跟蹤? – DNA

+0

也顯示你的堆棧跟蹤。 – basiljames

+0

你在'getDbPath()','getDbUser()'和'getDbPassword()'中看到了一些模式嗎?你認爲你可以做一些改變,只有一種方法?這只是一個提示。 – maba

回答

0

通過從構建路徑中刪除gnujaxp.jar解決了問題。

0

首先,我建議你不要在一行上鍊接太多的方法。將呼叫結構分成多行可以提高可讀性並簡化調試。

的exaple,改寫:

return (((Node) (eElement.getElementsByTagName("password").item(0).getChildNodes().item(0)).getNodeValue()); 

到:

NodeList rootEls = eElement.getElementsByTagName("password"); 
Node rootEl = rootEls.item(0) 
NodeList children = rootEl.getChildNodes(); 
Node passEl = children.item(0); 
return passEl.getNodeValue(); 

當你得到NullPointerException異常,與此代碼,可以提取從異常的行數更多的信息。其次,在這種情況下,查看用於Java的各種XML處理庫並找到允許使用XPath的庫可能是有用的,另請參見:this tutorial on Xpath

我希望這會有所幫助。隨意提出任何問題。

2

代碼太多!另外,按需閱讀配置文件並不是那麼有用。依靠實例變量會使您的代碼更難測試和理解,甚至在併發情況下可能不安全。你不需要所有這些類,方法和事物。這只是一個

public class Exercise { 

    public static void main(String[] args) throws XPathExpressionException { 

     XPath xpath = XPathFactory.newInstance().newXPath(); 
     InputSource in = new InputSource("res/config.xml"); 

     String user = xpath.evaluate("//sql/user/text()", in); 
     String password = xpath.evaluate("//sql/password/text()", in); 
     String path = xpath.evaluate("//sql/dataSource/text()", in); 

     Sql sql = new Sql(path, user, password); 
    } 

} 

事情你可以任選使你的代碼稍微複雜一些,通過存儲在Map<String, String>所有配置的,但真的是你最好使用像Properties一個通用的API,它能夠從XML加載。