7

i'm using XmlPullParser on Android but get getText return null. Why is this happening?

The code, the commented line gives the null

    ArrayList<String> titleList = new ArrayList<String>();
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput(this.getInputStream(), null);
        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equalsIgnoreCase(TITLE)) {
//                  MainActivity.itemsList.add(xpp.getText());
                    Log.d("XGamers", "a");
                }
            }``
            eventType = xpp.next();
        }
    } catch (XmlPullParserException e) {
        Log.e("XGamers", "XmlPullParserException in FeedParser");
    } catch (IOException e) {
        Log.e("XGamers", "IOException in FeedParser");
    }
Clepto
  • 685
  • 1
  • 6
  • 7
  • Does xpp.getName() give null or there is a NullPointerException when that line is executed? – Ryhan Jun 02 '13 at 09:39
  • I change the line to getText, it was wrong before.. A NullPointerException when is executed – Clepto Jun 02 '13 at 09:40

1 Answers1

10

Try this:

if (xpp.getName().equalsIgnoreCase(TITLE)) {
  if(xpp.next() == XmlPullParser.TEXT) { 
       MainActivity.itemsList.add(xpp.getText());
       Log.d("XGamers", "a");
  }
}

Also, make sure your itemsList is initialized.

Ryhan
  • 1,815
  • 1
  • 18
  • 22
  • Which part of it returns null? getName() or getText()? Also use && in your conditional statements. – Ryhan Jun 02 '13 at 20:50
  • You can to use `xpp.nextText()` method instead. Like `if (xpp.getName().equalsIgnoreCase(TITLE)) { text = xpp.nextText(); }` for `text`. – Ivan Black May 16 '15 at 14:52