0

I have an XML file as shown below. I've created a simple program to the reader this file and print in on the console.The problem that I'm facing is that it is giving me

java.lang.NullPointerException
    at xmlreader.main(xmlreader.java:46)

and also it is not reading some values.

 <problemType>
      <fleetSize>FINITE</fleetSize>
 </problemType>
 <vehicles>
      <vehicle>
           <id>1_1</id>
           <typeId>type_1</typeId>
           <startLocation>
                <id>[x=-20.2675][y=57.4797]</id>
                <coord x="-20.2675" y="57.4797"/>
           </startLocation>
           <endLocation>
                <id>[x=-20.2675][y=57.4797]</id>
                <coord x="-20.2675" y="57.4797"/>
           </endLocation>
           <timeSchedule>
                <start>0.0</start>
                <end>1.7976931348623157E308</end>
           </timeSchedule>
           <returnToDepot>true</returnToDepot>
      </vehicle>
      <vehicle>
      </vehicles>

I've created a java piece of code to read this file

    import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class xmlreader {

   public static void main(String argv[]) {

    try {

    File fXmlFile = new File("C:/Users/HP/Desktop/solution.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    //optional, but recommended
    //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

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

    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;

            System.out.println("id : " + eElement.getAttribute("id"));
            System.out.println("typeId : " + eElement.getElementsByTagName("typeId").item(0).getTextContent());
            System.out.println("startLocation : " + eElement.getElementsByTagName("startLocation").item(0).getTextContent());
            System.out.println("endLocation : " + eElement.getElementsByTagName("endLocation").item(0).getTextContent());
            System.out.println("timeSchedule : " + eElement.getElementsByTagName("timeSchedule").item(0).getTextContent());
                        System.out.println("returnTodepot : " + eElement.getElementsByTagName("returnTodepot").item(0).getTextContent());
        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}

when I tried to run the program, it is giving me errors "java.lang.NullPointerException" and also it seems that it not getting the values inside startLocation tags. Please help I'm kind of confused right now

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
Mattieu Kevin
  • 23
  • 1
  • 8
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – f1sh Feb 27 '18 at 10:16
  • Which line is line 46? – Rainer Feb 27 '18 at 10:17
  • @f1sh no duplicate mate, i want to read the values inside the startLocation tag – Mattieu Kevin Feb 27 '18 at 10:18
  • System.out.println("returnTodepot : " + eElement.getElementsByTagName("returnTodepot").item(0).getTextContent()); – Mattieu Kevin Feb 27 '18 at 10:19
  • 1
    typo on returnTodepot ... – Dogukan Zengin Feb 27 '18 at 10:21
  • 1
    @MattieuKevin yes, that is a duplicate. If you had put some effort into the duplicate suggestion, you would've found out that ``eElement.getElementsByTagName("returnTodepot")`` is ``null``. Which would've pointed you in the direction of the typo. – f1sh Feb 27 '18 at 10:21
  • @f1sh no it is not! the program is not giving me the values inside the tag startLocation, which contains other tags like id and coord. And the typo i have corrected but it is still giving me same thing. – Mattieu Kevin Feb 27 '18 at 10:25
  • @MattieuKevin then debug and find out what's happening. – f1sh Feb 27 '18 at 10:26

2 Answers2

1

You are getting nullpointer due to a typo.

you want an element : returnTodepot
while the element name is : returnToDepot

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
0

calling eElement.getTextContent() of returnToDepot is enough

System.out.println(eElement.getTextContent());


<returnToDepot>true</returnToDepot> 

returnToDepot not have any child elements. But in your code ,trying to access the 0the index,Which not exists.

eElement.getElementsByTagName("returnTodepot").item(0) 
Rajesh Gopu
  • 863
  • 9
  • 33