Android系统。致命异常:AsyncTask#1

时间:2016-03-31 20:18:15

标签: android android-asynctask

我需要一点帮助。我正在尝试使用AsyncTask(学习)。当我运行应用程序时,由于空指针异常导致AsyncTask#1出现致命异常错误。它说它在执行doInBackground时发生了。这是班级

public class Title extends AsyncTask <Void, Void, ArrayList> {

public ArrayList<HashMap<String, Object>> newsList;
HashMap<String, Object> hm;
public static String TITLE = "title";
public static String LINK = "link";

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected ArrayList doInBackground(Void... params) {
    try {
        URL url = new URL("some url");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();

        parser.setInput(getInputStream(url), "UTF_8");

        boolean insideItem = false;

        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (parser.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else if (parser.getName().equalsIgnoreCase("title")) {
                    if (insideItem){
                        hm = new HashMap<>();
                        hm.put(TITLE, parser.nextText());
                    }
                } else if (parser.getName().equalsIgnoreCase("link")) {
                    if (insideItem){
                        hm.put(LINK, parser.nextText());
                        newsList.add(hm);
                    }
                }
            } else if (eventType == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("item")) {
                insideItem = false;
            }

            eventType = parser.next();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return newsList;
}

这是我得到的错误

FATAL EXCEPTION: AsyncTask #1
Process: com.example.vladzakharo.rssreader, PID: 2573
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.vladzakharo.rssreader.Title.doInBackground(Title.java:59)
at com.example.vladzakharo.rssreader.Title.doInBackground(Title.java:19)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)

1 个答案:

答案 0 :(得分:1)

newsListnull,因为您从不为其分配值。如果您想避免NullPointerException,请不要在add()上致电newsList,直到您为newsList分配值(例如newsList=new ArrayList<HashMap<String, Object>>();)。