1

Sorry ahead of time I know very little about Java. I was handed this code to take over. Basically I get a NullPointerException on the line that is String[] patient = {eElement.getElementsByTagName("Name").item(0).getTextContent(), eElement.getElementsByTagName("ID").item(0).getTextContent(), eElement.getElementsByTagName("acquisitionDate").item(0).getTextContent(), eElement.getElementsByTagName("pages").item(0).getTextContent() }; when I run the code.

Here is the chunk that parses the XML

//parse array of string arrays from xml file that represent the patient list we expect to see, needs to be sorted by acquisition date, in reverse chronological order.
File fxmlFile = new File("patientList.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fxmlFile);

doc.getDocumentElement().normalize(); //should not be necessary but recommended (http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work)

NodeList nList = doc.getElementsByTagName("patient");

String[][] retList = new String[nList.getLength()][4];

for(int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
Element eElement = (Element)nNode;
String[] patient = {eElement.getElementsByTagName("Name").item(0).getTextContent(), eElement.getElementsByTagName("ID").item(0).getTextContent(), eElement.getElementsByTagName("acquisitionDate").item(0).getTextContent(), eElement.getElementsByTagName("pages").item(0).getTextContent() };

retList[i] = patient;
}

Here is the Improts, not sure if this is needed

package test.java.WebReview.Tests;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.Assert;
import main.java.WebReview.Pages.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

And here is the XML file

<?xml version="1.0"?>
<patients>
<patient>
<acquisitionDate>2015-04-03T10:03:49.0000000</acquisitionDate>
<id>9876543230</id>
<name>autoTestLast30, autoTestFirst30</name>
<pages>3</pages>
</patient>
</patients>
Seldon Stone
  • 474
  • 1
  • 5
  • 16
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – GriffeyDog Apr 20 '15 at 16:23
  • try splitting up the line into different strings and check which one is null. eg. String name =eElement.getElementsByTagName("Name").item(0).getTextContent(); Sting ID =... and so on – Ace McCloud Apr 20 '15 at 16:23
  • You know hardly anything from Java and somebody gave you this code to maintain? The line you point out has almost a dozen places where an NPE can be thrown. Split it in sublines, and check on which line it occurs then. – Stultuske Apr 20 '15 at 16:23
  • Did you use your debugger and step through the code to find out what's null? – tnw Apr 20 '15 at 16:23
  • Wow I just want to say you guys are great, within a minute I had an answer...maybe it was a really easy one haha. Thanks a lot! – Seldon Stone Apr 20 '15 at 16:29
  • I know you already have your answer, but for next time... There's one piece of information that you did not give us: You did not give us the exception stack trace. That "line" of code contains twelve method calls, and the stack trace would have told which one of them threw the exception. – Solomon Slow Apr 20 '15 at 17:18

3 Answers3

2
eElement.getElementsByTagName("Name")

evaluates to null (and raises the exception) in your example, please try:

eElement.getElementsByTagName("name") //case sensitive!

..and this is only the "tip of the iceberg", please watch for case sensitivity and spelling in all tag names!

xerx593
  • 12,237
  • 5
  • 33
  • 64
1

Format the code so that the offending line takes several lines, which would make it easier to pinpoint where the error is, or run in a debugger.

Check your tags, since name and Name, id and ID are not the same.

JP Moresmau
  • 7,388
  • 17
  • 31
1

The parameter to the method getElementsByTagName is case sensitive. So "Name" != "name", and "id" != "Id". Change either the xml file element name to match your code, or the case the values in your code to match the case of the elements in the xml file.

copeg
  • 8,290
  • 19
  • 28