473,396 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getting NULL POINTER EXCEPTION while exporting ResultSet to XML

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
1 1825
r035198x
13,262 8TB
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

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

Similar topics

1
by: jennifer johnson | last post by:
Hello All, I appreciate anyone's assistance. I wanted to validate user customizable field values so I changed my JSP page so that the form action would post existing form - action=<%=...
1
by: Mark | last post by:
Hi, I'm writing user controls with custom events. I have a parent custom event that exposes some abstract methods and some custom events. I have also created some new user controls that...
5
by: Tony Cheng | last post by:
for (int i=0; i<_request.Form.Count; i++ ) { string key = _request.Form.GetKey(i); if ( !key.Equals("formCode") && !key.Equals("formLanguage") && !key.Equals("__VIEWSTATE") &&...
3
by: chramprasad | last post by:
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ...
1
by: zahidkhan | last post by:
Hi All, Plz help me if you can..... I have a program something like this int main(int argc,char* argv) { try { int* p = NULL;
6
by: dev24 | last post by:
Hi all, I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another...
1
by: MRamaLakshmi | last post by:
hi, I am developing an application using Java Applet which will be uploading files. Its throwing Null Pointer exception while detecting the proxy when we are trying to load the applet using Java6....
0
by: j74 | last post by:
I was testing some concepts and I came across following scenarios where include file does not seem working. Is the test code issue or some ATG setting issue? Though DSP jsp include works but jhtml...
1
by: Merdith | last post by:
String x=""; Vector From = (Vector)session.getAttribute("DATESFROM"); int isize = From.size(); Vector To = (Vector)session.getAttribute("DATESTO"); int isize2 = To.size();...
5
by: iamdennisthomas | last post by:
Hi Guys i was developing a action servlet which is actulally getting the data from a form and putting it in the db but while executing i am getting a null pointer exception /* * Generated by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.