【问题标题】:NullPointerException in Asynctask's onPostExecute [duplicate]Asynctask 的 onPostExecute 中的 NullPointerException [重复]
【发布时间】:2016-02-20 04:58:08
【问题描述】:

我使用套接字来检索信息并且我使用了两次异步任务(第一个异步任务运行良好,但第二个异步任务的 onPostExceute 触发了NullPointerException) 下面是我的异步任务代码。

     // ******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
                    }

                }
            }

    }

}

cargohelper类如下

           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;
}


 }

堆栈跟踪在这里..

 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)

我知道这个问题被问了很多次,大多数答案都是关于初始化一个特定的东西,这里是关于初始化 List 数据类型。我什至已经初始化了它,但我不明白为什么它会抛出 NPE..

【问题讨论】:

  • 你初始化进度条了吗?
  • 您的 java 文件中的第 123 行出现异常。您能否在您的代码中标记该行。
  • 是的,我初始化了进度条
  • 抱歉第 123 行是私有类 SendMessage extends AsyncTask
  • 你有没有初始化你要在 onPostExecute 方法中清除的数组列表?

标签: java android multithreading collections android-asynctask


【解决方案1】:

问题是这个模型类 Cargo_Helper 它有 2 个静态属性,你设置为 null ,每次访问它都会得到 null 。

你可以试试这样的:

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);
    }
}

例子

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

然后你可以直接访问Cargo_Helper.list_AI,因为它是静态的。

我不知道这样的助手是否有帮助,但也许你可以扩展它

【讨论】:

  • 我已经在 preExecute 中初始化了列表,有错吗?
  • @george 这部分是错误的static List&lt;Cargo_Details&gt; list_AI=null; 你将它设置为空,所以你当然会在Cargo_Helper.list_ING.add() 上得到空指针
  • 我将 null 替换为 new ArrayList();现在它显示-java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on an null object reference
  • 谢谢你们......它已经解决......我在 Cargo_helper 类本身初始化了列表并进行了一些修改......它工作......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
相关资源
最近更新 更多