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: ");
}
}
onPostExecutein 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?for (int item: routes)it works now