在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)
解决方法:
如果在该行上引发了NPE:
Log.d("catArrayList:Size:New", ""+categoryArrayList.size());
那么它被抛出的原因是categoryArrayList为null.
检查该变量是否已正确初始化.
我也强烈怀疑您正在运行的代码与问题中向我们显示的代码不同.根据我的阅读,那时categoryArrayList不可能为null.如果是这样的话,那么NPE之前将在以下位置抛出两行:
Category c = categoryArrayList.get(0);
实际上,如果应用程序是多线程的,并且另一个线程与此运行此方法的线程并行地更新categoryArrayList,则由于竞争条件,该变量可能变为null.但是,我希望这只会偶尔发生.
我猜,另一种可能性是c.getTypeArrayList()调用正在更新categoryArrayList作为副作用.
第三种可能性是,NPE不会出现在您所说的那条线上.