title: Parsing XML with XmlPullParserFactory is crashing in while statement
body: <p>I am using a <code>XmlPullParserFactory</code> object to parse some <code>XML</code> that I receive from my server. For some reason it crashes at the <code>while</code> statement and I can't figure out why that would happen.</p>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;body&gt;
  &lt;item&gt;
    &lt;id&gt;115240&lt;/id&gt;
    &lt;title&gt;
      &lt;![CDATA[Student Evangelism Team Leaves for Fiji]]&gt;
    &lt;/title&gt;
    &lt;created&gt;20130821-15:50&lt;/created&gt;
    &lt;author&gt;
      &lt;![CDATA[Staff Writer]]&gt;
    &lt;/author&gt;
    &lt;summary&gt;
      &lt;![CDATA[On August 21, Service, Justice, and Missions Coordinator Fabio Maia and ten
        Pacific Union College students left for an eighteen-day evangelism mission in
        Fiji with ShareHim and Quiet Hour Ministries.]]&gt;
    &lt;/summary&gt;
    &lt;body_text&gt;
      &lt;![CDATA[&lt;p&gt;On August 21, Service, Justice, and Missions Coordinator
        Fabio Maia and ten Pacific Union College students left for an eighteen-day
        evangelism mission in Fiji with ShareHim and Quiet Hour Ministries. Each
        student will be placed in a specific community where they will present the
        story of Christ’s love and sacrifice each evening. Many of the students are
        active in the college’s praise and worship teams and brought their instruments
        so they can share God’s love through music.&lt;/p&gt;
        &lt;p&gt;Before the group departed, the students gathered in
        President Heather J. Knight’s office for a special blessing prayer led by
        Pastor Mark Witas, the lead pastor of the PUC Church. Pastor Witas included
        special prayer that God lead the group in doing good in His name. &lt;a&gt;&lt;/a&gt;The group also
        bonded by sharing a meal together before heading to the airport for their
        flight to Fiji.&lt;/p&gt;
        &lt;p&gt;For many PUC students, summer is the perfect opportunity to
        devote time to ministry and service. While some students work at summer camps
        or serve as literature evangelists, other groups get the experience of a
        lifetime by sharing the gospel and serving local communities in places around
        the world. The PUC community is invited to join in prayer for the team travelling
        to Fiji as well as other student groups currently on short-term mission trips
        to Nicaragua and Brazil.&lt;/p&gt;]]&gt;
    &lt;/body_text&gt;
    &lt;url&gt;http://www.puc.edu/news/archives/2013/student-evangelism-team-leaves-for-fiji&lt;/url&gt;
    &lt;images&gt;
      &lt;image url="http://www.puc.edu/__data/assets/image/0014/115241/fiji.jpg" url_2x="http://www.puc.edu/__data/assets/image/0014/115241/varieties/2x.jpg" thumb_small="http://www.puc.edu/__data/assets/image/0014/115241/varieties/thumb.jpg" thumb_medium="http://www.puc.edu/__data/assets/image/0014/115241/varieties/thumb_large.jpg" thumb_large="http://www.puc.edu/__data/assets/image/0014/115241/varieties/thumb_large.jpg" /&gt;
    &lt;/images&gt;
    &lt;/item&gt;
  ... lots more item nodes
  &lt;/body&gt;
  </code></pre>
  
  <p>Java:</p>
  
  <pre><code>class PUCNewsItem
  {
  
      public String title;
      public String summary;
      public String body;
      public String url;
      public String imageUrl;
  
  }
  
  public class PUCDatasource extends Activity {
  
      public static ArrayList&lt;PUCNewsItem&gt; getPUCNews() throws IOException {
  
              String url = "http://api.mysite.com/list";
              InputStream is = downloadUrl(url);
              XmlPullParserFactory pullParserFactory;
  
              try {
                  pullParserFactory = XmlPullParserFactory.newInstance();
                  XmlPullParser parser = pullParserFactory.newPullParser();
                  parser.setInput(is, null);
  
                  ArrayList&lt;PUCNewsItem&gt; items = null;
                  int eventType = parser.getEventType();
                  PUCNewsItem item = null;
                  Log.d("Debug: ", "Start");
                  while (eventType != XmlPullParser.END_DOCUMENT){
                      String name = null;
                      switch (eventType){
                          case XmlPullParser.START_DOCUMENT:
                              Log.d("Debug: ", "Start doc");
                              items = new ArrayList&lt;PUCNewsItem&gt;();
                              break;
                          case XmlPullParser.START_TAG:
                              name = parser.getName();
                              if (name == "item"){
                                  item = new PUCNewsItem();
                              } else if (item != null){
                                  if (name == "title"){
                                      item.title = parser.nextText();
                                  } else if (name == "summary"){
                                      item.summary = parser.nextText();
                                  } else if (name == "body_text"){
                                      item.body = parser.nextText();
                                  }  
                              }
                              break;
                          case XmlPullParser.END_TAG:
                              name = parser.getName();
                              if (name.equalsIgnoreCase("item") &amp;&amp; item != null) {
                                  items.add(item);
                              }
                              break;
                      }//end switch
  
                      eventType = parser.next();
  
                  }//end while
  
                  return items;
  
              } catch (XmlPullParserException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              return null;
  
          }//end
  
  }//end class
  </code></pre>
  
  <p>Errors:</p>
  
  <pre><code>09-03 16:31:50.100: W/System.err(16727): Invalid stream or encoding: java.io.IOException: Stream is closed (position:START_DOCUMENT null@1:1) caused by: java.io.IOException: Stream is closed; nested exception is:
  09-03 16:31:50.108: W/System.err(16727): java.io.IOException: Stream is closed
  09-03 16:31:50.108: W/System.err(16727):    at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:158)
  09-03 16:31:50.108: W/System.err(16727):    at libcore.io.Streams.readSingleByte(Streams.java:41)
  09-03 16:31:50.108: W/System.err(16727):    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:128)
  09-03 16:31:50.108: W/System.err(16727):    at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1623)
  09-03 16:31:50.108: W/System.err(16727):    at com.puc.mobile.PUCDatasource.getPUCNews(PUCDatasource.java:40)
  09-03 16:31:50.108: W/System.err(16727):    at com.puc.mobile.MainActivity$DownloadPUCNews.doInBackground(MainActivity.java:94)
  09-03 16:31:50.108: W/System.err(16727):    at com.puc.mobile.MainActivity$DownloadPUCNews.doInBackground(MainActivity.java:1)
  09-03 16:31:50.108: W/System.err(16727):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
  09-03 16:31:50.108: W/System.err(16727):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  09-03 16:31:50.108: W/System.err(16727):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  09-03 16:31:50.108: W/System.err(16727):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
  09-03 16:31:50.108: W/System.err(16727):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  09-03 16:31:50.108: W/System.err(16727):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  09-03 16:31:50.108: W/System.err(16727):    at java.lang.Thread.run(Thread.java:856)
  09-03 16:31:50.108: D/AndroidRuntime(16727): Shutting down VM
  09-03 16:31:50.108: W/dalvikvm(16727): threadid=1: thread exiting with uncaught exception (group=0x40aba210)
  09-03 16:31:50.108: E/AndroidRuntime(16727): FATAL EXCEPTION: main
  09-03 16:31:50.108: E/AndroidRuntime(16727): java.lang.NullPointerException
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at com.puc.mobile.MainActivity$DownloadPUCNews.onPostExecute(MainActivity.java:103)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at com.puc.mobile.MainActivity$DownloadPUCNews.onPostExecute(MainActivity.java:1)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at android.os.AsyncTask.finish(AsyncTask.java:602)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at android.os.Handler.dispatchMessage(Handler.java:99)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at android.os.Looper.loop(Looper.java:137)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at android.app.ActivityThread.main(ActivityThread.java:4697)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at java.lang.reflect.Method.invokeNative(Native Method)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at java.lang.reflect.Method.invoke(Method.java:511)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
  09-03 16:31:50.108: E/AndroidRuntime(16727):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Any ideas on why the crash is happening? Should I somehow be specifying the name of my roote node?</p>