korunos
korunos

Reputation: 798

NullPointerException while parsing xml at equal tags

My XML-File:

<job_id>000273E060E87000C0001323FA21427120150602153306</job_id>
<job_id>000273E060E87000C0001323FA21427120150602153342</job_id>
<job_id>000273E060E87000C0001323FA21427120150602153419</job_id>

I get a NullPointerException here:

protected ArrayList<HashMap<String, String>> doInBackground(URL... urls) {
        if (cd.isConnectingToInternet()) {
            XMLParser xmlParser = new XMLParser();
            String xml = xmlParser.getXMLFromUrl("http://www.example.de/android.php?f=1200");
            Document doc = xmlParser.getDomElement(xml);
            writeToFile(xml, "xmlfilesnames");

            NodeList noteListFileNames = doc.getElementsByTagName("tasklist");

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

                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) noteListFileNames.item(i);

                map.put("job_id", xmlParser.getValue(e, "job_id"));
                xmlFileNames.add(map);

            }

at: map.put("job_id", xmlParser.getValue(e, "job_id")); when the for loop run for second time.

Stacktrace:

java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.NullPointerException
        at xmlParsing.XMLParser.getValue(XMLParser.java:88)
        at de.example.app.ListViewActivity$DownloadXMLFilesName.doInBackground(ListViewActivity.java:298)
        at de.example.app.ListViewActivity$DownloadXMLFilesName.doInBackground(ListViewActivity.java:281)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)             at java.lang.Thread.run(Thread.java:841)

The XML-File looks like:

<?xml version="1.0" encoding="utf-8"?>
<tasklist>
<job_id>000273E060E87000C0001323FA21427120150602153306</job_id>
<job_id>000273E060E87000C0001323FA21427120150602153342</job_id>
<job_id>000273E060E87000C0001323FA21427120150602153419</job_id>
</tasklist>

First Loop

xmlFileNames:

000273E060E87000C0001323FA21427120150602153419

map:

000273E060E87000C0001323FA21427120150602153419

Second Loop xmlFileNames:

000273E060E87000C0001323FA21427120150602153342
000273E060E87000C0001323FA21427120150602153342

map:

000273E060E87000C0001323FA21427120150602153342

Third Loop

xmlFileNames

000273E060E87000C0001323FA21427120150602153419
000273E060E87000C0001323FA21427120150602153419
000273E060E87000C0001323FA21427120150602153419

map:

000273E060E87000C0001323FA21427120150602153419

Upvotes: 0

Views: 303

Answers (1)

slarge
slarge

Reputation: 644

Should be less than not less than or equal to in the for loop

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

should be

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

In addition to your further question:

Try changing:

Element e = (Element) noteListFileNames.item(i);

map.put("job_id", xmlParser.getValue(e, "job_id"));
xmlFileNames.add(map);

to:

Element e = (Element) noteListFileNames.item(i);

NodeList children = e.getChildNodes();

for (int j = 0; j < children.getLength(); j++) {
    if (children.item(j).getNodeName().equals("job_id")) {
        map.put(children.item(j).getNodeName(),
        children.item(j).getTextContent());
        xmlFileNames.add(map);
    }
}

or something similar. Not sure which parser you are using.

Upvotes: 1

Related Questions