尝试调用接口方法' void com.bustracker.AsyncTaskCallback.onAsyncTaskFinished(java.util.ArrayList)'在null对象引用上

时间:2015-05-31 20:31:27

标签: android

我试图将结果从doInBackground传递给onPostExecute()方法,但是我收到了这个错误:

  

java.lang.NullPointerException:尝试调用接口方法' void com.bustracker.AsyncTaskCallback.onAsyncTaskFinished(java.util.ArrayList)'在空对象引用上

我无法理解我已经在onPostExecute检查arrayList是否为空但是当路由The output of routes: [3,6,8]的输出时应用程序崩溃但是当arrayList为空时,应用程序不会崩溃?!

我该如何解决?

我感谢任何帮助。

PostData类:

public class PostData {
    String jSONString;
    private AsyncTaskCallback callback;


    public PostData(AsyncTaskCallback callback) {
        this.callback = callback;
    }

    public String getjSONString() {
        return jSONString;

    }

    public void setjSONString(String jSONString) {
        this.jSONString = jSONString;
    }

    public void post_data(String jSONString, Context context) {
        this.jSONString = jSONString;

        new MyAsyncTask(context).execute(jSONString);
        // new MyAsyncTask(context).cancel(true);

    }

    class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<Integer>> {

            ArrayList<Integer> routes = new ArrayList<Integer>();

            @Override
            protected ArrayList<Integer> doInBackground(String... params) {
                // TODO Auto-generated method stub
                BufferedReader reader = null;

                try {

                    URL myUrl = new URL(
                            "https://bustracker.rhcloud.com/webapi/test");
                    HttpURLConnection conn = (HttpURLConnection) myUrl
                            .openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(10000);
                    conn.setReadTimeout(10000);
                    conn.connect();

                    StringBuilder sb = new StringBuilder();
                    reader = new BufferedReader(new InputStreamReader(
                            conn.getInputStream()));
                    String line;

                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");

                    }
                    System.out.println("StringBuilder: " +sb);

                    Gson gson = new Gson();
                    JSONStore data = gson.fromJson(sb.toString(), JSONStore.class);
                    routes = data.getRoutes();
                    System.out.println("The output of routes: " +routes);



                } catch (IOException e) {

                    e.printStackTrace();
                    return null;
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                            return null;
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }

                return null;

            }

            protected void onPostExecute(ArrayList<Integer> result) {

                if (routes != null && !routes.isEmpty()) {
                    callback.onAsyncTaskFinished(routes);
                } else {
                    Log.e("123", "Avoiding null pointer in the AvailableRoutes class, the routes are null!!!");
                }
            }

        }
}

1 个答案:

答案 0 :(得分:0)

  

java.lang.NullPointerException:尝试调用接口方法   “无效   com.bustracker.AsyncTaskCallback.onAsyncTaskFinished(java.util.ArrayList中)”   在空对象引用上

看起来您的界面实例为空。初始化AsyncTaskCallback实例。