Getting NullPointerException in ArrayList. I'm using the line below to log size of ArrayList but I always get NPE.
What could be the reason?
Log.d("catArrayList:Size:New", ""+categoryArrayList.size());
Here is some more code :
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));
}
Log says:
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)
If the NPE is thrown on this line:
Log.d("catArrayList:Size:New", ""+categoryArrayList.size());
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.
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);
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.
I guess, another possibility is that the c.getTypeArrayList() call is updating categoryArrayList as a side-effect.
And a third possibility is that the NPE doesn't occur at the line you said.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With