onPostExecute() 中的 ArrayList 空指针异常 [英] ArrayList Null Pointer Exception in onPostExecute()

查看:31
本文介绍了onPostExecute() 中的 ArrayList 空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ArrayList 中获取 NullPointerException.我正在使用下面的行来记录 ArrayList 的大小,但我总是得到 NPE.

Getting NullPointerException in ArrayList. I'm using the line below to log size of ArrayList but I always get NPE.

可能是什么原因?

 Log.d("catArrayList:Size:New", ""+categoryArrayList.size());

这里还有一些代码:

    protected void onPostExecute(Boolean result) {
        dialog.cancel();                 

        Log.d("catArrayList:Size", ""+categoryArrayList.size());
        Log.d("typArrayList:Size", ""+typeArrayList.size());
        Log.d("serArrayList:Size", ""+serviceArrayList.size());

        Log.d("cArrayList:Size", ""+cArrayList.size());
        Log.d("tArrayList:Size", ""+tArrayList.size());
        Log.d("sArrayList:Size", ""+sArrayList.size());

        Category c = categoryArrayList.get(0);      
        typeArrayList = c.getTypeArrayList();
        Log.d("catArrayList:Size:New", ""+categoryArrayList.size());

        for(int i=0;i<typeArrayList.size();i++) {
            tArrayList.add(typeArrayList.get(i).getName());
        }

        spinner1.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                 android.R.layout.simple_spinner_dropdown_item,
                 cArrayList));

        spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                android.R.layout.simple_spinner_dropdown_item,
                tArrayList));

        spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                android.R.layout.simple_spinner_dropdown_item,
                sArrayList));

     }

日志说:

 09-12 11:05:54.585: D/catArrayList:Size(30919): 2
09-12 11:05:54.585: D/typArrayList:Size(30919): 2
09-12 11:05:54.585: D/serArrayList:Size(30919): 2
09-12 11:05:54.585: D/cArrayList:Size(30919): 2
09-12 11:05:54.590: D/tArrayList:Size(30919): 2
09-12 11:05:54.590: D/sArrayList:Size(30919): 2
09-12 11:05:54.590: D/AndroidRuntime(30919): Shutting down VM
09-12 11:05:54.590: W/dalvikvm(30919): threadid=1: thread exiting with uncaught exception (group=0x40f9f2a0)
09-12 11:05:54.590: E/AndroidRuntime(30919): FATAL EXCEPTION: main
09-12 11:05:54.590: E/AndroidRuntime(30919): java.lang.NullPointerException
09-12 11:05:54.590: E/AndroidRuntime(30919):    at com.example.modulewise.CategoryActivity$JSONAsyncTask.onPostExecute(CategoryActivity.java:178)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at com.example.modulewise.CategoryActivity$JSONAsyncTask.onPostExecute(CategoryActivity.java:1)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at android.os.AsyncTask.finish(AsyncTask.java:631)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at android.os.Looper.loop(Looper.java:137)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at android.app.ActivityThread.main(ActivityThread.java:4921)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at java.lang.reflect.Method.invoke(Method.java:511)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
09-12 11:05:54.590: E/AndroidRuntime(30919):    at dalvik.system.NativeStart.main(Native Method)

推荐答案

如果在这一行抛出 NPE:

If the NPE is thrown on this line:

    Log.d("catArrayList:Size:New", ""+categoryArrayList.size());

然后它被抛出的原因是 categoryArrayListnull ...在程序中的那个点.

then the reason it is being thrown is that categoryArrayList is null ... at that point in the program.

检查该变量是否被正确初始化.

Check that that variable is being initialized correctly.

我还强烈怀疑您正在运行的代码与您在问题中向我们展示的不同.根据我的阅读,此时 categoryArrayList 不可能为 null.如果是,则 NPE 之前会在以下位置抛出两行:

I also strongly suspect that the code that you are running is different to what you have shown us in the question. By my reading, it is impossible for categoryArrayList to be null at that point. If it was, then an NPE would have been thrown two lines previously at:

    Category c = categoryArrayList.get(0);

实际上,如果应用程序是多线程的,并且另一个线程与运行此方法的线程并行更新categoryArrayList,那么由于竞争条件,该变量可能变为空.但是,我希望这只会偶尔发生.

Actually, if the application is multi-threaded, and another thread updates categoryArrayList in parallel with this thread running this method, then the variable could become null due to a race-condition. However, I would expect that this would only happen very occasionally.

我猜,另一种可能性是 c.getTypeArrayList() 调用正在更新 categoryArrayList 作为副作用.

I guess, another possibility is that the c.getTypeArrayList() call is updating categoryArrayList as a side-effect.

第三种可能性是 NPE 不会出现在你说的那一行.

And a third possibility is that the NPE doesn't occur at the line you said.

这篇关于onPostExecute() 中的 ArrayList 空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆