2

I am sending 2 kind of requests to the server once if the route is known then the data will be transmitted and inserted into the table. The other one if the route is unknown then the server will be requested to provide the android app with the route as long as there is available data to calculate the route otherwise route is 0.

in onPostExecute I am checking wether the 'routeList' is not empty and not null though I am getting this error:

Error:

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
  • There are 2 references to onPostExecute in your stack trace: one in 'PostData' class on line 1 and one in 'PostData' again but on line 132. Are your looking at the right 'PostData' method to track the error? Commented May 25, 2015 at 18:14
  • After deleting the for Loop for (int item: routes) it works now Commented May 25, 2015 at 18:47

2 Answers 2

4

you should ask first if routes != null like:

// 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!!!");

    }

The && evaluate the first condition and then the second, so if routes is null and you do routes.IsEmpty() it will throw an error. But if you ask first if routes is null and it is then it does not evaluate the second condition.

Sign up to request clarification or add additional context in comments.

1 Comment

I have changed it but nothing changed the error still appears.
0

I think the problem is in this line

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

You are trying to extract list where the routes is null or empty

Why don't you try like this ?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.