在ArrayList中获取NullPointerException
。我使用下面的行来记录ArrayList的大小,但我总是得到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)
答案 0 :(得分:3)
如果NPE被抛到这一行:
Log.d("catArrayList:Size:New", ""+categoryArrayList.size());
然后抛出它的原因是categoryArrayList
是null
...在程序中的那一点。
检查该变量是否正确初始化。
我还强烈怀疑您运行的代码与您在问题中向我们展示的代码不同。根据我的阅读,categoryArrayList
在此时不可能null
。如果是,那么NPE之前会抛出两行:
Category c = categoryArrayList.get(0);
实际上,如果应用程序是多线程的,并且另一个线程与运行此方法的此线程并行更新categoryArrayList
,则该变量可能由于竞争条件而变为null。但是,我希望这只会偶尔发生。
我想,另一种可能性是c.getTypeArrayList()
来电正在更新categoryArrayList
作为副作用。
第三种可能性是NPE 不会出现在你说的那条线上。