By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,780 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,780 IT Pros & Developers. It's quick & easy.

getting NULL POINTER EXCEPTION while exporting ResultSet to XML

P: 5
Im trying this java code to export data from database using sql query, But getting a null pointer exception. getting no clue to remove it . Im getting this exception at line no 26.

Expand|Select|Wrap|Line Numbers
  1. try {
  2.  
  3.     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  4.     DocumentBuilder builder = factory.newDocumentBuilder();
  5.     Document doc = builder.newDocument();
  6.     Element results = doc.createElement("Results");
  7.     doc.appendChild(results);
  8.  
  9.     Class.forName("oracle.jdbc.OracleDriver");
  10.     Connection con = DriverManager
  11.             .getConnection("jdbc:oracle:thin:@localhost:1521:XE", "hr", "hr");
  12.  
  13.     ResultSet rs = con.createStatement().executeQuery("select * from departments");
  14.  
  15.     ResultSetMetaData rsmd = rs.getMetaData();
  16.     int colCount = rsmd.getColumnCount();
  17.  
  18.     while (rs.next()) {
  19.         Element row = doc.createElement("Row");
  20.         results.appendChild(row);
  21.         for (int i = 1; i <= colCount; i++) {
  22.             String columnName = rsmd.getColumnName(i);
  23.             Object value = rs.getObject(i);
  24.             Element node = doc.createElement(columnName);
  25.             row.appendChild(node);
  26.                 node.appendChild(doc.createTextNode(value.toString()));
  27.         }
  28.     }
  29.  
  30.  
  31.     DOMSource domSource = new DOMSource(doc);
  32.     TransformerFactory tf = TransformerFactory.newInstance();
  33.     Transformer transformer = tf.newTransformer();
  34.     transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  35.     transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  36.     transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
  37.     StringWriter sw = new StringWriter();
  38.     StreamResult sr = new StreamResult(sw);
  39.     transformer.transform(domSource, sr);
  40.  
  41.     System.out.println(sw.toString());
  42.  
  43.  
  44.     con.close();
  45.     rs.close();
  46.  
  47. } catch (Exception e) {
  48.     e.printStackTrace();
  49. }
Also I have tested
Expand|Select|Wrap|Line Numbers
  1. doc
has a "null" value , right after its assignment.

Help required. Thank You
Mar 30 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
So look at the code that assigns that doc value and check its documentation to see what conditions it returns a null value.
Mar 31 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.