2012-09-25 34 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加载。