0

Problem: XML parsing returns a null pointer exception, unable to retrieve the text from the program tag in xml. Stepping through code shows: (This is from the debug statements as shown in code below)

err StartTag entry
err StartTag record
err Text 4

Line that calls parser (also the one that causes nullpointer, pail is a freshly created container):

pail = XmlParsee.parsee(getResources().openRawResource(R.raw.testprogramlist));         

My XML File:

<entry>
    <record>
        <program>Program 1(English)</program>
    </record>
    <record>
        <program>Program 2(Mandarin)</program>
    </record>
</entry>

My parser (I tried both getText() and nextText(), both return the same issue):

public class XmlParsee {
// jealousy has invaded! Generic xml parser that returns a container
public static Container parsee(InputStream inputriver) {
    Container container = null;

    try {
        // get new parser object from factory
        XmlPullParser parsee = XmlPullParserFactory.newInstance().newPullParser();
        parsee.setInput(inputriver, null);

        int eventType = parsee.getEventType();
        // while xml still has more, we read.

        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT: {
                    //these comments to be replaced with logging functions, if you desire
                    //wokay, we begin
                    container = new Container();
                    System.err.println("doc start");
                    break;
                }
                case XmlPullParser.END_DOCUMENT:{
                    //wokay, we end.
                    break;
                }
                case XmlPullParser.START_TAG:{
                    //new tag! first we get the tag's name
                    String tag = parsee.getName();
                    System.err.println("StartTag "+parsee.getName());
                    //then we check, individually, what the tag is to confirm content
                    //if <program>
                    if(tag.equalsIgnoreCase(Container.PROGRAM)){
                        System.err.println("Text "+parsee.TEXT);
                        //container.addProgramList(parsee.getText());
                    }
                    //if <>                     
                    break;
                }
            }
            //done with this line, next!
            eventType = parsee.next();
        }
    } catch (Exception e) {
        container = null;
    }

    return container;
}

Any ideas? I've been banging my head at this since morning =\

1 Answer 1

1

The nextText() works for me;

    //System.err.println("Text "+parsee.TEXT);
    System.err.println("Text "+parsee.nextText());

the output:

...
09-13 14:51:22.035: W/System.err(12080): doc start
09-13 14:51:22.035: W/System.err(12080): StartTag entry
09-13 14:51:22.045: W/System.err(12080): StartTag record
09-13 14:51:22.065: W/System.err(12080): StartTag program
09-13 14:51:24.285: W/System.err(12080): Text Program 1(English)
09-13 14:56:57.505: W/System.err(12080): StartTag record
09-13 14:56:57.515: W/System.err(12080): StartTag program
09-13 14:56:58.075: W/System.err(12080): Text Program 2(Mandarin)
...

Regards

Ziteng Chen

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, my bad. Identified the wrong place that was erroneous. Feeling rather silly now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.