Quantcast
Channel: Null Pointer error RSS feed - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Null Pointer error RSS feed

$
0
0

I'm trying to build a basic RSS Feed. I have no errors showing up in code however when I run the application it crashes with the following error:

FATAL EXCEPTION: AsyncTask #1java.lang.RuntimeException: An error occured while executing doInBackground()

This is my MainActivity.java code:

public class MainActivity extends ListActivity {List<String> headlines;List<String>links;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //initialize variables    headlines = new ArrayList<String>();    links = new ArrayList<String>();    new PostTask().execute();    //binding data to list    ArrayAdapter <String>adapter=  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, headlines);    setListAdapter(adapter);}private InputStream getInputStream(URL url) {    try{        return url.openConnection().getInputStream();    }catch(IOException e){        return null;    }}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {    Uri uri = Uri.parse(links.get(position));    Intent intent = new Intent(Intent.ACTION_VIEW, uri);    startActivity(intent);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.activity_main, menu);    return true;}//ASYNCH CLASSprivate class PostTask extends AsyncTask<String, Integer, String>{    @Override    protected String doInBackground(String... arg0) {        try{            //link to data source            URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");            //Set up parser            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();            factory.setNamespaceAware(false);            XmlPullParser xpp = factory.newPullParser();            //get XML from input stream            xpp.setInput(getInputStream(url), "UTF_8");            //Keep track of which tag inside of XML            boolean insideItem = false;            //Loop through the XML file and extract data required            int eventType = xpp.getEventType();            while(eventType != XmlPullParser.END_DOCUMENT){                if(xpp.getName().equalsIgnoreCase("item")){                    insideItem = true;                }else if(xpp.getName().equalsIgnoreCase("title")){                    if(insideItem){                        //Get title                        headlines.add(xpp.nextText());                    }                }else if(xpp.getName().equalsIgnoreCase("link")){                    if(insideItem){                        //Get link                        links.add(xpp.nextText());                    }                }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){                insideItem = false;            }            eventType = xpp.next();//move to next element        }    }catch(MalformedURLException e){        e.printStackTrace();    }catch(XmlPullParserException e){        e.printStackTrace();    }catch(IOException e){        e.printStackTrace();    }        return null;    }    public void onPostExecute(URL url) {    }}

}

Can anyone point me to where I am going wrong? Many thanks!

LOG CAT ERRORS:

01-11 17:27:50.061: E/AndroidRuntime(7746): FATAL EXCEPTION: AsyncTask #101-11 17:27:50.061: E/AndroidRuntime(7746): java.lang.RuntimeException: An error occured while executing doInBackground()01-11 17:27:50.061: E/AndroidRuntime(7746):     at android.os.AsyncTask$3.done(AsyncTask.java:299)01-11 17:27:50.061: E/AndroidRuntime(7746):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)01-11 17:27:50.061: E/AndroidRuntime(7746):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)01-11 17:27:50.061: E/AndroidRuntime(7746):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)01-11 17:27:50.061: E/AndroidRuntime(7746):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)01-11 17:27:50.061: E/AndroidRuntime(7746):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)01-11 17:27:50.061: E/AndroidRuntime(7746):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)01-11 17:27:50.061: E/AndroidRuntime(7746):     at java.lang.Thread.run(Thread.java:856)01-11 17:27:50.061: E/AndroidRuntime(7746): Caused by: java.lang.NullPointerException01-11 17:27:50.061: E/AndroidRuntime(7746):     at com.example.simplerss.MainActivity$PostTask.doInBackground(MainActivity.java:89)01-11 17:27:50.061: E/AndroidRuntime(7746):     at com.example.simplerss.MainActivity$PostTask.doInBackground(MainActivity.java:1)01-11 17:27:50.061: E/AndroidRuntime(7746):     at android.os.AsyncTask$2.call(AsyncTask.java:287)01-11 17:27:50.061: E/AndroidRuntime(7746):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)01-11 17:27:50.061: E/AndroidRuntime(7746):     ... 4 more

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>