尝试调用虚方法' boolean java.util.ArrayList.isEmpty()'在null对象引用上

时间:2015-05-25 18:11:01

标签: android

如果路由已知,我将向服务器发送一种请求,然后将数据传输并插入到表中。另一个如果路线未知,则只要有可用数据来计算路线,服务器将被要求提供Android应用程序,否则路线为0。

onPostExecute中的

我正在检查“路线列表”。虽然我收到此错误,但不是空的并且不为空:

错误:

05-25 20:33:29.107: E/AndroidRuntime(1946): FATAL EXCEPTION: main
05-25 20:33:29.107: E/AndroidRuntime(1946): Process: com.bustracker, PID: 1946
05-25 20:33:29.107: E/AndroidRuntime(1946): java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
05-25 20:33:29.107: E/AndroidRuntime(1946):     at com.bustracker.PostData$MyAsyncTask.onPostExecute(PostData.java:131)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at com.bustracker.PostData$MyAsyncTask.onPostExecute(PostData.java:1)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at android.os.AsyncTask.finish(AsyncTask.java:632)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at android.os.Looper.loop(Looper.java:145)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at android.app.ActivityThread.main(ActivityThread.java:5944)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at java.lang.reflect.Method.invoke(Native Method)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at java.lang.reflect.Method.invoke(Method.java:372)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-25 20:33:29.107: E/AndroidRuntime(1946):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

onPostExecute():

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

@Override
    protected void onPostExecute(Void result) {
        for (int item: routes){
            System.out.println("The output from the onPostExecute:  "+item);
        }
        // Intent with Conetxt of the Asyntask class and
        if (!routes.isEmpty() && (routes != null)) {
            // if(routes !=null ){
            Intent intent = new Intent(mContext, MainActivity.class);
            intent.putIntegerArrayListExtra("stop_route", routes);
             intent.putExtra("stop_distance", distance);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mContext.startActivity(intent);

        } else {
            Log.e("123", "Avoiding null pointer, the routes are null!!!");

        }

    }

onNewIntent():

@Override
protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    super.onNewIntent(intent);

    // getIntent() should always return the most recent
    setIntent(intent);

    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey("stop_route")) {


        double distance = extras.getDouble("stop_distance");
        System.out.println("The distance is: "+ distance);

        if (distance <= 15) {
            stop_popup(extras);

        } else {
            final ArrayList<Integer> routeList = extras
                    .getIntegerArrayList("stop_route");
            route_number = routeList.get(0);
            System.out.println("The route number is: " + route_number);
        }

    } else {
        System.out.println("The intent is empty: ");
    }

}

2 个答案:

答案 0 :(得分:3)

你首先应该问路线!= null如:

// Intent with Conetxt of the Asyntask class and
    if (routes != null && !routes.isEmpty()) {
        Intent intent = new Intent(mContext, MainActivity.class);
        intent.putIntegerArrayListExtra("stop_route", routes);
         intent.putExtra("stop_distance", distance);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        mContext.startActivity(intent);

    } else {
        Log.e("123", "Avoiding null pointer, the routes are null!!!");

    }

&amp;&amp;评估第一个条件,然后评估第二个条件,因此如果routes为null并且你执行routes.IsEmpty(),它将抛出一个错误。 但是如果你首先询问路由是否为空,那么它就不会评估第二个条件。

答案 1 :(得分:0)

我认为问题出在这一行

for (int item: routes){
            System.out.println("The output from the onPostExecute:  "+item);
        }

您正在尝试提取路由为空或空的列表

为什么不尝试这样做?

for(routes!=null  && routes.size>0)
for (int item: routes){
            System.out.println("The output from the onPostExecute:  "+item);
        }