RE: NullPointer Exception error while reading a XML

Hi –

I am getting following error while trying to read and display a content from an XML file.

Exception in thread “main” java.lang.NullPointerException
at firstPackage.ReadXml.main(ReadXml.java:33)

Code:


public class ReadXml {
public static void main(String[] args) throws ParserConfigurationException,

SAXException, IOException {
File xmlFile = new File(System.getProperty("user.dir") + "//Meals.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(xmlFile);
NodeList nlXml = doc.getChildNodes();

Node nXml = nlXml.item(0);

Element element = (Element) nXml;
System.out.println("Meal: "

+ element.getElementsByTagName("Elements").item(0)

.getTextContent());
}
}

Could you please help with this error? Thanks so much!

Aruna

Aruna Default Asked on November 10, 2014 in Selenium WebDriver.
Add Comment
3 Answers

Hi Aruna,I think this is the mistake File xmlFile = new File(System.getProperty(“user.dir”) + “//Meals.xml”);  change this line toFile xmlFile = new File(System.getProperty(“user.dir”) + “\\Meals.xml”);

Train Answered on November 10, 2014.

Hi Mallik –

Actually I am using a UBUNTU system, so I had to use the forward slash.

on November 11, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.