获取空指针异常将数据从数据库导出到xml

时间:2013-03-29 19:30:16

标签: java sql xml dom jdbc

我正在尝试使用sql查询从数据库导出数据,但是获取空指针异常。不知道要删除它。我在Bold字体的行中得到了这个例外。

try {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);

    Class.forName("oracle.jdbc.OracleDriver");
    Connection con = DriverManager
            .getConnection("jdbc:oracle:thin:@localhost:1521:XE", "hr", "hr");

    ResultSet rs = con.createStatement().executeQuery("select * from departments");

    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    while (rs.next()) {
        Element row = doc.createElement("Row");
        results.appendChild(row);
        for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            Element node = doc.createElement(columnName);
            row.appendChild(node);
            **node.appendChild(doc.createTextNode(value.toString()));**
        }
    }


    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);

    System.out.println(sw.toString());


    con.close();
    rs.close();

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

堆栈追踪:

java.lang.NullPointerException
at org.ProjectXML.ui.DataBaseToXML.main(DataBaseToXML.java:61)

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

诀窍是在while循环中添加这段简单的代码:

if (value != null)
  node.appendChild(doc.createTextNode(value.toString()));
else
  node.appendChild(doc.createTextNode(""));
  row.appendChild(node);

完整代码:

public static String ConvertToXML1(ResultSet rs, String view)
  throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement(view);
    doc.appendChild(results);

    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    while (rs.next()) {
      Element row = doc.createElement("Row");
      results.appendChild(row);
      for (int i = 1; i <= colCount; i++) {
        String columnName = rsmd.getColumnName(i);
        Object value = rs.getObject(i);
        Element node = doc.createElement(columnName);
        // The added code is here
        if (value != null)
          node.appendChild(doc.createTextNode(value.toString()));
        else
          node.appendChild(doc.createTextNode(""));
        row.appendChild(node);
      }
    }

    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();

    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);

    transformer.transform(domSource, sr);

    return sw.toString();
  }