darsh2 / MultipleImageSelect

Android library that provides for multiple image selection.
Apache License 2.0
300 stars 112 forks source link

Another Null Pointer Exception on Runnable #16

Open WaiLynnZaw opened 8 years ago

WaiLynnZaw commented 8 years ago

I am not sure if it is related with issue #14 and I got this error. (My android version is - 6.0.1 and I am using v.0.0.4 of multiple image select library)

java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
                                                                                 at java.io.File.fixSlashes(File.java:183)
                                                                                 at java.io.File.<init>(File.java:130)
                                                                                 at com.darsh.multipleimageselect.activities.AlbumSelectActivity$AlbumLoaderRunnable.run(AlbumSelectActivity.java:353)
                                                                                 at java.lang.Thread.run(Thread.java:818)
darsh2 commented 8 years ago

As far as I know, it is not related with issue #14 . I could recreate this error by doing image = null. Problem is in line 353 in AlbumSelectActivity (v0.0.4), String image is null. Does this always occur or is it only in specific use cases?

WaiLynnZaw commented 8 years ago

What do you mean "image = null". It is only occur in one device when I test with about 3 devices. Thanks

darsh2 commented 8 years ago

In the following snippet:

String album = cursor.getString(cursor.getColumnIndex(projection[0]));
String image = cursor.getString(cursor.getColumnIndex(projection[1]));

/*
It may happen that some image file paths are still present in cache,
though image file does not exist. These last as long as media
scanner is not run again. To avoid get such image file paths, check
if image file exists.
 */
file = new File(image);

by adding

image = null;

just before the statement

file = new File(image);

I could recreate the error in the device I tested on. This occurs probably because

String image = cursor.getString(cursor.getColumnIndex(projection[1])); 

cursor.getString returns null. Quoting from the documentation:

"The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined."

Will look into this. Quick fix solution would be:

if (image == null) {
    return;
}
file = new File(image);

I'd appreciate any pull requests to fix this.