views:

117

answers:

1

I have the following code in a few activities and it reads and parses XML files fine but in two activities when reading small XML files I get a null pointer exception and can't figure out what the problem is:

while (eventType != XmlResourceParser.END_DOCUMENT) {
    if ((eventType == XmlResourceParser.START_TAG) ||
        (eventType == XmlResourceParser.TEXT)) {
        // Get the name of the tag (eg scores or score)
        String strName = xmlSymptoms.getName();
        if (helpFound == 1){
            helpText  = xmlSymptoms.getText();
            helpFound = 0;
        } else if (strName.equals("string") && (eventType == XmlResourceParser.START_TAG)) {
            helpFound = 1;
        }

    }
    eventType = xmlSymptoms.next();
}

The null pointer exception occurs when I am reading in "<string>" which I am able to read in with the same code in other activities. In this activity, I am able to read in the other XML tokens fine until I get to "<string>".

Here is my debug window

Thread [<1> main] (Suspended)
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2628
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 ActivityThread$H.handleMessage(Message) line: 2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626 NativeStart.main(String[]) line: not available [native method]
Thread [<6> Binder Thread #2] (Running) Thread [<5> Binder Thread #1] (Running)

I have LogCat running, but nothing shows up in it.

Here is some XML that works:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
<key>A</key>
<array>
        <string>ABANDONED, feeling:     </string>
        <string>ABSCESS:        </string>
</array>
</dict>
</plist>

and here is some XML that doesn't work:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"   "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
  <key>HOWTOTAKE</key>  
  <array>
  <string> Put two pellets in 8 oz bottle of water.</string>
  </array>
</dict>
</plist>

One last note: When I get to the "<string>" field, getName() returns "null" instead of "string". Obviously trying to tell me something, I just can't figure out why "null" is returned.

Any ideas how to debug this?

A: 

I figured out what the problem was. Since I am looking for XmlResourceParser.TEXT, strName.equals("string") is null when (eventType == XmlResourceParser.TEXT) is true. Changing the code to only check for (strName.equals("string") when (eventType == XmlResourceParser.START_TAG) is true, fixed the problem as shown in the code below:

while (eventType != XmlResourceParser.END_DOCUMENT) {
    if ((eventType == XmlResourceParser.START_TAG) ||
        (eventType == XmlResourceParser.TEXT)) {
        // Get the name of the tag (eg scores or score)
        String strName = xmlSymptoms.getName();
        if (helpFound == 1){
            helpText  = xmlSymptoms.getText();
            helpFound = 0;
        } else if ( (eventType == XmlResourceParser.START_TAG)) {
            if (strName.equals("string") ){
                helpFound = 1;
            }
        }

    }
    eventType = xmlSymptoms.next();
}
}
James Testa