XmlPullParser returning null for .getName()

+3 David Frenzel · January 8, 2016
I'm kinda stuck with an issue where my app catches a NullPointerException because .getName() returns null. 

Here's my Parser class: 

package de.gymnasium_beetzendorf.vertretungsplan;

import android.content.Context;
import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class XMLParser {

static final String TAG = ".vertretungsplan";
// header tags
static final String headerTag = "kopf"; // header for timetable
static final String dateTag = "datum";
static final String affectedClassesTag = "aenderungk"; // shows affected classes

// actual information tags
static final String timetableTag = "haupt";
static final String changeTag = "aktion"; // contains the timetable
static final String courseTag = "klasse"; // contains a change
static final String periodTag = "stunde";
static final String subjectTag = "fach";
static final String teacherTag = "lehrer"; // new/changed room
static final String roomTag = "raum"; // additional information such as assignments etc
static final String infoTag = "info";


public static List<Schoolday> parseXMLInput(Context context) {
List<Schoolday> result;
result = new ArrayList<>();

Schoolday currentDay = new Schoolday();
Subject currentSubject = new Subject();
List<Subject> currentSubjectList = new ArrayList<>();

try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();

// open fis to the file to get the information
FileInputStream fis = context.openFileInput("temp.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

// set input for the parser
xpp.setInput(reader);

// set starting event type
int eventType = xpp.getEventType();

// go
Log.i(TAG, "xpp states: " +
"\nstart_document:" + String.valueOf(XmlPullParser.START_DOCUMENT) +
"\nend_document:" + String.valueOf(XmlPullParser.END_DOCUMENT) +
"\nstart_tag:" + String.valueOf(XmlPullParser.START_TAG) +
"\nend_tag:" + String.valueOf(XmlPullParser.END_TAG) +
"\ntext:" + String.valueOf(XmlPullParser.TEXT)
);

String tag = "", text;
while (eventType != XmlPullParser.END_DOCUMENT) {
tag = xpp.getName();
text = xpp.getText();
Log.i(TAG, "event type: " + String.valueOf(eventType) + "\ntag: " + tag + "\ntext(tag): " + text);

switch (eventType) {
case XmlPullParser.START_TAG:
if (tag.equalsIgnoreCase(headerTag)) {
currentDay = new Schoolday();
Log.i(TAG, "Schoolday instantiated, tag: " + tag);
} else if (tag.equalsIgnoreCase(changeTag)) {
currentSubject = new Subject();
Log.i(TAG, "Subject instantiated, tag: " + tag);
}
break;
case XmlPullParser.TEXT:
if (tag.equalsIgnoreCase(dateTag)) {
String dateString = xpp.getText().substring(0, 9);
currentDay.setDate(dateString);
/*SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY);
Date date;
try {
date = formatter.parse(dateString);
} catch (ParseException e) {
Log.i(TAG, "Parse exception", e);
date = null;
}
currentDay.setDate(date);*/
} else if (tag.equalsIgnoreCase(affectedClassesTag)) {
String[] affectedClasses = xpp.getText().split(", ");
currentDay.setChanges(Arrays.asList(affectedClasses));
} else if (tag.equalsIgnoreCase(courseTag)) {
currentSubject.setCourse(xpp.getText());
} else if (tag.equalsIgnoreCase(periodTag)) {
int period;
try {
period = Integer.parseInt(xpp.getText());
} catch (Exception e) {
Log.i(TAG, "conversion to int failed", e);
period = 0;
}
currentSubject.setPeriod(period);
} else if (tag.equalsIgnoreCase(subjectTag)) {
currentSubject.setSubject(xpp.getText());
} else if (tag.equalsIgnoreCase(teacherTag)) {
currentSubject.setTeacher(xpp.getText());
} else if (tag.equalsIgnoreCase(roomTag)) {
currentSubject.setRoom(xpp.getText());
} else if (tag.equalsIgnoreCase(infoTag)) {
currentSubject.setInfo(xpp.getText());
}

case XmlPullParser.END_TAG:
if (tag.equalsIgnoreCase(changeTag)) {
currentSubjectList.add(currentSubject);
} else if (tag.equalsIgnoreCase(timetableTag)) {
currentDay.setSubjects(currentSubjectList);
}
break;
}
// move to the next iteration -- finally reach END_DOCUMENT
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
Log.i(TAG, "PullParserException", e);
} catch (FileNotFoundException e) {
Log.i(TAG, "File not found", e);
} catch (IOException e) {
Log.i(TAG, "IOException", e);
} catch (NullPointerException e) {
Log.i(TAG, "Some data is missing (NullPointer)", e);
}

return result;
}
}

and here my XML structure:

<vp>
<kopf>
<titel>Freitag, 11. Dezember 2015 (B-Woche)Kurzf. Aktual. beachten</titel>
<schulname>Gymnasium Beetzendorf</schulname>
<datum>11.12.2015, 07:11</datum>
<kopfinfo>
<aenderungk>05 A, 05 B, 06 D, 07 A, 07 B, 08 A, 08 B, 08 C, 08 D, 09 C, 10 C, 11 B, 11 C, 12 A, 12 B, 12 C</aenderungk>
</kopfinfo>
</kopf>
<haupt>
<aktion>
<klasse>05 A</klasse>
<stunde>4</stunde>
<fach>Mat</fach>
<lehrer>PLA</lehrer>
<raum>Ch</raum>
<info>für Eng SFK </info>
</aktion>
</haupt>
</vp>

this is my logcat:

01-07 23:32:04.172 28095-28191/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: Connection geöffnet
01-07 23:32:04.673 28095-28191/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: Buffered-/InputStream aktiv
01-07 23:32:04.674 28095-28191/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: File-/BufferedOutput aktiv
01-07 23:32:04.858 28095-28191/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: Dauerte - 689ms 
                                                                                             Buffer: 12741
01-07 23:32:04.862 28095-28095/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: xpp states: 
                                                                                            start_document:0
                                                                                            end_document:1
                                                                                            start_tag:2
                                                                                            end_tag:3
                                                                                            text:4
01-07 23:32:04.862 28095-28095/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: event type: 0
                                                                                            tag: 
                                                                                            text(tag): null
01-07 23:32:04.862 28095-28095/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: event type: 2
                                                                                            tag: vp
                                                                                            text(tag): null
01-07 23:32:04.862 28095-28095/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: event type: 4
                                                                                            tag: null
                                                                                            text(tag): 
                                                                                              
01-07 23:32:04.863 28095-28095/de.gymnasium_beetzendorf.vertretungsplan I/.vertretungsplan: Some data is missing (NullPointer)
                                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
                                                                                                at de.gymnasium_beetzendorf.vertretungsplan.XMLParser.parseXMLInput(XMLParser.java:91)
                                                                                                at de.gymnasium_beetzendorf.vertretungsplan.MainActivity$DownloadFileFromURL.onPostExecute(MainActivity.java:164)
                                                                                                at de.gymnasium_beetzendorf.vertretungsplan.MainActivity$DownloadFileFromURL.onPostExecute(MainActivity.java:115)
                                                                                                at android.os.AsyncTask.finish(AsyncTask.java:636)
                                                                                                at android.os.AsyncTask.access$500(AsyncTask.java:177)
                                                                                                at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


I know the xml is in German but I think that's insignificant for this issue to potentially get help from someone here! 

It's probably just something stupid but I've been sitting here for hours messing around with the code but haven't gotten a solution -.- 

Thanks!

Post a Reply

Replies

Oldest  Newest  Rating
0 Gary Whitney · January 8, 2016
So go to line 91 in your code and wrap the call to getName() in a try/catch block with an if (getName()!=null).

P.S. I got line 91 from you logCat.
0 David Frenzel · January 14, 2016
Thanks! This avoided the error but didn't actually help with solving the whole issue. 

In case someone else is interested (this might be obvious to some of you but it wasn't for me):
When the eventType is .START_TAG or .END_TAG .getText() will always return null. So you need to save the text during XmlPullParser.TEXT to a variable and then use it later under START_TAG or END_TAG.

My PullParser is now working :)
  • 1

Java / Android Development

106,928 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator