Question

I'm developing an application on the Android system which involves parsing some xml stuff. The problem is that the program is crashing and I've haven't found out the problem. I have an Activity which has a private class which extends asynctask. This is the code:

URL url = new URL(XML_INIT_ADRESS);
XmlPullParser xpp =  XmlPullParserFactory.newInstance().newPullParser();
xpp.setInput(url.openConnection().getInputStream(), null);

int pullParserState = xpp.getEventType();

while (pullParserState != XmlPullParser.END_DOCUMENT){
    if (xpp.getName().equals("signupclients") && pullParserState == XmlPullParser.START_TAG){ //Error!!!               
        while(!xpp.getName().equals("signupclients") && pullParserState != XmlPullParser.END_TAG){
            if (xpp.getName().equals("client")) {
                clientNames.add(xpp.getAttributeValue(0));
            }
            pullParserState = xpp.next();
        }
        pullParserState = xpp.next();
    }
}    

As you can see I've commented on the first if-statement that if you comment away that and the code its containing the code will work. I could add that it's in Eclipse and I catch all errors.

String XML_INIT_ADRESS = "http://www.johanstenberg.se/tests/app/start.xml"

It should be a valid XML-document. Please help because I'm clueless but I want to see it work with the XMLPullParser. I call the asynctask which in the onCreate() method like this:

new UpdateDatabase().execute();

Where UpdateDatabase is the name of the private class extending Asynctask.

Thanks in advance!

Logcat:

12-05 18:38:43.664: W/KeyCharacterMap(525): No keyboard for id 0
12-05 18:38:43.664: W/KeyCharacterMap(525): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-05 18:38:44.854: I/AndroidRuntime(525): AndroidRuntime onExit calling exit(0)
12-05 18:38:45.154: D/dalvikvm(535): GC freed 750 objects / 54888 bytes in 51ms
12-05 18:38:45.584: W/dalvikvm(535): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
12-05 18:38:45.594: E/AndroidRuntime(535): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
12-05 18:38:45.604: E/AndroidRuntime(535): java.lang.RuntimeException: An error occured while executing doInBackground()
12-05 18:38:45.604: E/AndroidRuntime(535):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
1    2-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.lang.Thread.run(Thread.java:1096)
12-05 18:38:45.604: E/AndroidRuntime(535): Caused by: java.lang.NullPointerException
12-05 18:38:45.604: E/AndroidRuntime(535):  at com.discountapp.johanaugust.UpdatingDB$UpdateDatabase.doInBackground(UpdatingDB.java:119)
12-05 18:38:45.604: E/AndroidRuntime(535):  at com.discountapp.johanaugust.UpdatingDB$UpdateDatabase.doInBackground(UpdatingDB.java:1)
12-05 18:38:45.604: E/AndroidRuntime(535):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-05 18:38:45.604: E/AndroidRuntime(535):  ... 4 more
12-05 18:38:45.654: I/dalvikvm(535): threadid=7: reacting to signal 3
12-05 18:38:45.654: E/dalvikvm(535): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
Was it helpful?

Solution

If you are getting an exception from this line:

if(xpp.getName().equals("signupclients") && pullParserState == XmlPullParser.START_TAG)

Then my best guess is that xpp.getName() is returning null, and you are trying to invoke .equals() from that null reference.

You should be checking the eventType from your XmlPullParser before trying to do something with it. Take a look at this snippet from the JavaDoc:

         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
          } else if(eventType == XmlPullParser.START_TAG) {
              System.out.println("Start tag "+xpp.getName());
          } else if(eventType == XmlPullParser.END_TAG) {
              System.out.println("End tag "+xpp.getName());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          }
          eventType = xpp.next();
         }

Its hard to give a more definitive answer without more details (like a stack trace).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top