Debug Nullpointerexception
Solution 1:
If your NullPointerException
is being caused by this
home.listFiles(newFileExtensionFilter()).length
then
home.listFiles(newFileExtensionFilter())
seems to be null
. This is likely because the listFiles
function can return null
:
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
Which implies that MEDIA_PATH
is not the directory path that you think it is.
The out-of-bounds exception is caused by this line:
mp.setDataSource(songsList.get(songIndex).get("songPath"));
Meaning songIndex
does not exist in songsList
. As stated in your exception:
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
You can't reference element 0, which is the first element, in a zero-element list.
Since songsList
is being added to only as a result of the data found in the MEDIA_PATH
directory, and MEDIA_PATH
is somehow erroneous... Hopefully when you solve the problem with MEDIA_PATH
, both problems will be solved.
Solution 2:
Please replace your code like this:
if (home.listFiles(new FileExtensionFilter())!=null && home.listFiles(new FileExtensionFilter()).length > 0)
{
}
and check listFiles
is containg no list. i.e. either your filepath is not not proper and file doesn't contain any song. Please make sure your sdcard conatins any .mp3 file.
Solution 3:
I'm pretty sure that in Manifest
you forgot the permission to READ EXTERNAL STORAGE
, and that's why you got the IndexOutOfBoundsException
.
Post a Comment for "Debug Nullpointerexception"