I used sockets to retrieve information and i used asynctask twice(1st async task works well but 2nd asynctask's onPostExceute triggers a NullPointerException
)
Below is my asynctask code.
// ******THE FIRST LINE IS GIVEN AN ERROR [LINE:123]
private class SendMessage extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
//CargoHelper is a class where the below two lists are present
Cargo_Helper.list_AI = new ArrayList<Cargo_Details>();
Cargo_Helper.list_ING = new ArrayList<Cargo_Details>();
if(count!=1) {
listView.setVisibility(View.INVISIBLE);
Log.d("sri", "onPreExecute" + count);
al.clear();
}
if(count == 1){
listView.setVisibility(View.VISIBLE);
Log.d("sri", "onPreExecute" + count);
id = "C";
}
}
@Override
protected String doInBackground(Void... params) {
l1:
try {
// this part works fine
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("connection closed");
return message;
}
private void updateListView(String message) {
//updates listview this part works well
}
}
@Override
protected void onPostExecute(String message) {
progressBar.setVisibility(View.INVISIBLE);
cargo.setVisibility(View.VISIBLE);
if (message != null) {
listView.setVisibility(View.VISIBLE);
if(count!=1) {
updateListView(message);
count = 1;
Log.d("sri","onPostExecute"+count);
}
else{
//Toast.makeText(getApplicationContext(),"List obtained:" + al,Toast.LENGTH_LONG).show();
Log.d("sri", "In onPostExecute" + message);
// sort_the_objects_for_LV(message);
if(isDes)
return;
if(message!=null || message!=" ") {
String[] arr = message.split(" ");
if (arr[1].equals("AI4565"))
//******* THIS LINE IS ERRORED..I HAVE INTIALIZED THE LIST IN PREEXECUTE ITSELF [LINE :287]
Cargo_Helper.list_AI.add(new Cargo_Details(arr[0], arr[1]
Long.getLong(arr[2]), arr[3], Integer.parseInt(arr[4])));
else if (arr[1].equals("ING1234")) {
Cargo_Helper.list_ING.add(new Cargo_Details(arr[0], arr[1], Long.getLong(arr[2]), arr[3], Integer.parseInt(arr[4])));
}
// to be continued for all flights
// to be continued for all flights
}
}
}
}
}
The cargohelper class is as follows
public class Cargo_Helper {
static List<Cargo_Details> list_AI=null;
static List<Cargo_Details> list_ING=null;
public static List<Cargo_Details> get_AirIndia_Cargo(){ return list_AI;
}
public static List<Cargo_Details> get_Indigo_Cargo(){
return list_ING;
}
}
The stack trace is here..
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException at airhost.project_2_phase.All_Flight_No_Obtain_Activity$SendMessage.onPostExecute(All_Flight_No_Obtain_Activity.java:287)
at airhost.project_2_phase.All_Flight_No_Obtain_Activity$SendMessage.onPostExecute(All_Flight_No_Obtain_Activity.java:123)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5409)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
I knew this question was asked multiple times and most of the answers were about intializing a particular thing and here its about intializing the List datatype.I have even intialized it but i can't understand why it throws me NPE..