0

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..

george
  • 339
  • 3
  • 12

1 Answers1

0

The problem is this model class Cargo_Helper It has 2 static properties that you set to null , every time you access it you will get null .

You can try something like this:

public class Cargo_Helper  {

    //you cargo details
    public static class Cargo_Details {
        public Cargo_Details() {

        }
    }

    public static List<Cargo_Details> list_AI = new ArrayList<Cargo_Details>();
    public static List<Cargo_Details> list_ING = new ArrayList<Cargo_Details>();
    static {
        //some code here
    }

    private static void addAirIndiaCargo(Cargo_Details cargo) {
        list_AI.add(cargo);
    }

    private static void addAirIndigoCargo(Cargo_Details cargo) {
        list_ING.add(cargo);
    }
}

example

Cargo_Helper.list_AI.add(new Cargo_Details());

Then you can acess Cargo_Helper.list_AI directly since its static.

I dont know if such helper is helpful but perhaps you can extend it

meda
  • 45,103
  • 14
  • 92
  • 122
  • I have intialized the lists in preExecute, is that wrong? – george Feb 20 '16 at 05:27
  • @george this part is wrong `static List list_AI=null;` you set it to null, so of course you will get nullpointer on `Cargo_Helper.list_ING.add()` – meda Feb 20 '16 at 05:31
  • I replaced the null to new ArrayList(); now it shows-java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference – george Feb 20 '16 at 05:40
  • Thank you guys ..it is solved ...i intialized the lists at the Cargo_helper class itself and made some modifications ..it worked ... – george Feb 20 '16 at 08:57