Run-Time Error (Null Pointer Exception)

Hi all,

Through this forum, I would like to ask anyone on the run-time error message that i got while using your JAVA source code to read an XML file.

The source code which is posted by JAVAGALAXY.COM is as below:

import java.io.File;

import org.w3c.dom.Document;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

public class ReadandWriteXml{

public static void main (String argv [])throws DOMException

{

try {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document doc = docBuilder.parse (new File("c:\\property.xml"));

// normalize text representation

doc.getDocumentElement ().normalize ();

System.out.println ("Root element of the doc is " +

doc.getDocumentElement ().getNodeName());

String FirstChildNode= doc.getDocumentElement().getFirstChild().getNextSibling().getNodeName();

System.out.println(FirstChildNode);

NodeList listOfPersons = doc.getElementsByTagName(FirstChildNode);

int totalPersons = listOfPersons.getLength();

System.out.println("Total no of nodes : " + totalPersons);

for(int s=0; s<listOfPersons.getLength() ; s++)

{

Node firstPersonNode = listOfPersons.item(s);

if(firstPersonNode.getNodeType () == Node.ELEMENT_NODE)

{

Element firstPersonElement = (Element)firstPersonNode;

NodeList ListOfNodes= doc.getDocumentElement().getFirstChild().getNextSibling().getChildNodes();

System.out.println("");

for(int i=0;i<ListOfNodes.getLength();i++)

{

NodeList firstNameList = firstPersonElement.getElementsByTagName (ListOfNodes.item(i).getNodeName());

Element firstNameElement = (Element)firstNameList.item(0);

NodeList textFNList = firstNameElement.getChildNodes();

System.out.print(ListOfNodes.item(i).getNodeName());

System.out.println(" " +

((Node)textFNList.item(0)).getNodeValue().trim());

}

}

}

}catch (Throwable t) {

t.printStackTrace ();

}

//System.exit (0);

}//end of main

}

My XML file to be read is as below:

><MediaProperty

Title="default"

Author="default"

AuthorId="1"

Copyright="Blue Orange Pte Ltd"

Rating="restricted"

Description="default"

StartTime="2006-11-16 12:15:56"

EndTime="2006-11-16 12:20:33"

NoOfStillFrameFiles="470"

NoOfVideoFiles="0"

NoOfAudioFiles="0"

CategoryId="1"

ForumId="1"

PackageCodeName="kappa"

PostProcessingRequest="Y"

ReplyPostId="0"

/>

Run-Time Error which I got is as below:

Root element of the doc is MediaProperty

java.lang.NullPointerException

at ReadandWriteXml.main(ReadandWriteXml.java:31)

Lastly, I hope that someone could reply to me soon.

I would like to say many thanks in advance too.

regards,

Devit[

[3295 byte] By [novice4javaa] at [2007-11-26 16:01:30]
# 1
The stack trace says that the exception occurred at line 31. Which of the lines in that unindented mess is line 31?
DrClapa at 2007-7-8 22:23:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
String FirstChildNode= doc.getDocumentElement().getFirstChild().getNextSibling().getNodeName();
novice4javaa at 2007-7-8 22:23:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Hi DrClap,

I would like to Thank you for your reply.

Sorry for my bad presentation of the source code.

The following is the indented source code:

import java.io.File;

import org.w3c.dom.Document;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

public class ReadandWriteXml{

public static void main (String argv [])throws DOMException

{

try {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document doc = docBuilder.parse (new File("c:\\book.xml"));

// normalize text representation

doc.getDocumentElement ().normalize ();

System.out.println ("Root element of the doc is " +

doc.getDocumentElement ().getNodeName());

String FirstChildNode= doc.getDocumentElement().getFirstChild().getNextSibling().getNodeName();

System.out.println(FirstChildNode);

NodeList listOfPersons = doc.getElementsByTagName(FirstChildNode);

int totalPersons = listOfPersons.getLength();

System.out.println("Total no of nodes : " + totalPersons);

for(int s=0; s<listOfPersons.getLength() ; s++)

{

Node firstPersonNode = listOfPersons.item(s);

if(firstPersonNode.getNodeType () == Node.ELEMENT_NODE)

{

Element firstPersonElement = (Element)firstPersonNode;

NodeList ListOfNodes= doc.getDocumentElement().getFirstChild().getNextSibling().getChildNodes();

System.out.println("");

for(int i=0;i<ListOfNodes.getLength();i++)

{

NodeList firstNameList = firstPersonElement.getElementsByTagName (ListOfNodes.item(i).getNodeName());

Element firstNameElement = (Element)firstNameList.item(0);

NodeList textFNList = firstNameElement.getChildNodes();

System.out.print(ListOfNodes.item(i).getNodeName());

System.out.println(" " +

((Node)textFNList.item(0)).getNodeValue().trim());

}

}

}

}

catch (Throwable t) {

t.printStackTrace ();

}

}//end of main

}//end class ReadandWriteXml

regards,

Devit>

novice4javaa at 2007-7-8 22:23:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Be carefull...

The code in line 31 you post isString FirstChildNode= doc.getDocumentElement().getFirstChild().getNextSibling().getNodeName();

get the sibling node of the first child node of the root node. This is not the first child element!!!!

Many xml parsers creates a "white space" node when a break line is encountered. If your xml contains a break line between the root node and his first child node then there is a reason to skip the first child, but if your xml doesn't contains any break line (or some others stuff such as comments) then your first element is also the first child node.

To test it I suggest you to make a loop that skips all non-element nodes. A code to do that looks like:

String FirstChildNode = null; //null means no element node found

NodeList children = doc.getDocumentElement().getChildNodes();

int j = 0;

while (j < children.getLength() && FirstChildNode == null)

{

Node child = children.item(j);

if (child.getNodeType() == Node.ELEMENT_NODE)

{

FirstChildNode = child.getNodeName();

}

j++;

}

topfoxya at 2007-7-8 22:23:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...