I am getting a NullPointerException in the following line commented below.
Sometimes I get it (especially on first instance), sometimes I don’t. So I am trying to solve this once and for all.
I retrieve the file path from an image database and then use bitmap decode on it. I am sure there are values in the database.
Thank you.
ImageDB imagedb = new ImageDB(this);
imagedb.open();
ArrayList<String> image_list = new ArrayList<String>();
image_list.clear();
if(imagedb.open() != null){
Cursor tagcursor = imagedb.retrieveTag(tag);
if(tagcursor.moveToFirst()){
do{
image_list.add(tagcursor.getString(tagcursor.getColumnIndex(tag)));
}
while(tagcursor.moveToNext());
}
tagcursor.close();
imagedb.close();
}
if(image_list != null && !image_list.isEmpty()){
Collections.shuffle(image_list);
if(image_list.get(0).toString() != null){ // nullpointerexception here or below if I comment here
imagepath = image_list.get(0).toString();
}
}
This is the stack trace, but its just as a warning, the program still runs:
04-16 19:03:31.603: W/System.err(1355): java.lang.NullPointerException
04-16 19:03:31.613: W/System.err(1355): at com.MucaaApps.imageUpdate.ImageService.setTag(ImageService.java:422)
04-16 19:03:31.613: W/System.err(1355): at com.MucaaApps.ImageUpdate.imageService.setImageIcon(ImageService.java:377)
04-16 19:03:31.623: W/System.err(1355): at com.MucaaApps.ImageUpdate.ImageService.onStart(ImageService.java:237)
04-16 19:03:31.623: W/System.err(1355): at android.app.Service.onStartCommand(Service.java:306)
04-16 19:03:21.552: W/System.err(1355): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2873)
04-16 19:03:21.552: W/System.err(1355): at android.app.ActivityThread.access$3500(ActivityThread.java:119)
04-16 19:03:21.552: W/System.err(1355): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
04-16 19:03:21.552: W/System.err(1355): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 19:03:21.552: W/System.err(1355): at android.os.Looper.loop(Looper.java:123)
04-16 19:03:21.552: W/System.err(1355): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-16 19:03:21.563: W/System.err(1355): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 19:03:21.563: W/System.err(1355): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 19:03:21.573: W/System.err(1355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-16 19:03:21.573: W/System.err(1355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-16 19:03:21.582: W/System.err(1355): at dalvik.system.NativeStart.main(Native Method)
Simple Answer. You added the value null to your ArrayList.
you can fix it so: