0

Im trying to make a newsreader on android 4.1 using the ROME Feed Reader. However, I keep getting errors when my app is trying to call a newsfeed. I've been busy implementing Asynctask, because 4.1 doesn't let me retrieve rss-feeds on the main thread.

ERROR/AndroidRuntime(27669): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.cloud.backend.android/com.google.cloud.backend.android.FeedActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.google.cloud.backend.android.FeedListAdapter.getCount(FeedListAdapter.java:32)
        at android.widget.ListView.setAdapter(ListView.java:466)
        at com.google.cloud.backend.android.FeedActivity.createList(FeedActivity.java:34)
        at com.google.cloud.backend.android.FeedActivity.onCreate(FeedActivity.java:17)
        at android.app.Activity.performCreate(Activity.java:5206)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)

Here's my code (partially):

public class FeedListAdapter extends BaseAdapter
{
    private SyndFeed feed;
    private Activity context;

    public FeedListAdapter( Activity context )
    {
        this.context = context;
        String feedUrl = context.getIntent().getExtras().getString("Feed");
        new FeedRetrieval().execute(feedUrl);
    }

    public int getCount()
    {
        return feed.getEntries().size();
    }

    ... (shortened)

    private class FeedRetrieval extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... params) {
            RssAtomFeedRetriever feedRetriever = new RssAtomFeedRetriever();
            feed = feedRetriever.getMostRecentNews(params[0]);
            return null;
        }
    }

Edit: I've changed getCount() to:

if (feed != null) {
    return feed.getEntries().size();
}
return 0;

But now i get:

07-28 01:14:34.182: ERROR/AndroidRuntime(29175): FATAL EXCEPTION: AsyncTask #1
07-28 01:14:34.242: ERROR/android.os.Debug(433): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

Logcat doesn't show what causes AsyncTask to fail. This makes even less sense to me :(

Praetore
  • 99
  • 9

2 Answers2

0

Your feed variable is null until the AsyncTask finishes executing. If you call getCount before it is finished, it will try to access feed, which is null at that time.

Either return a default value or offer a way to check if the data is actually present.

Femaref
  • 60,705
  • 7
  • 138
  • 176
0

Solution: I've got it. The example is hosted on my GitHub: https://github.com/praetore/AndroidROMEFeedReader

Praetore
  • 99
  • 9