droid droid - 4 months ago 9
Java Question

Null Pointer Exception at 'if' statement

Im getting furious, because Im still getting an error at my code:

myVar = getFiles();
(...)
public ArrayList<HashMap<String, String>> getFiles(){
File dir = new File(new String("/sdcard/"));

if (dir.listFiles(new myFilter()).length > 0) {
for (File file : dir.listFiles(new myFilter())) {
(...)
}
}
return myList;
}


at if statement:

if (dir.listFiles(new myFilter()).length > 0) {


This is what i get:

01-13 22:28:42.313: W/dalvikvm(13776): threadid=1: thread exiting with uncaught exception (group=0x40c5a1f8)
01-13 22:28:42.318: E/AndroidRuntime(13776): FATAL EXCEPTION: main
01-13 22:28:42.318: E/AndroidRuntime(13776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.remotedevice/com.example.remotedevice.Player}: java.lang.NullPointerException
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.os.Looper.loop(Looper.java:137)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-13 22:28:42.318: E/AndroidRuntime(13776): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 22:28:42.318: E/AndroidRuntime(13776): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 22:28:42.318: E/AndroidRuntime(13776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-13 22:28:42.318: E/AndroidRuntime(13776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-13 22:28:42.318: E/AndroidRuntime(13776): at dalvik.system.NativeStart.main(Native Method)
01-13 22:28:42.318: E/AndroidRuntime(13776): Caused by: java.lang.NullPointerException
01-13 22:28:42.318: E/AndroidRuntime(13776): at com.example.remotedevice.Player.getPlayList(Player.java:120)
01-13 22:28:42.318: E/AndroidRuntime(13776): at com.example.remotedevice.Player.onCreate(Player.java:56)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.app.Activity.performCreate(Activity.java:4470)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-13 22:28:42.318: E/AndroidRuntime(13776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-13 22:28:42.318: E/AndroidRuntime(13776): ... 11 more


I've read this topic : Null Pointer Exception after checking with 'if' statement but havent find answear, which I could use to my code :/ I've tried few 'solutions' but still getting this error :( PLEASE HELP!

Answer

File listFiles() may return null for example if the file is not a directory. You'll need to check for != null before accessing the length field to prevent the NPE.

Now, why isn't /sdcard a directory? On many devices it is a symbolic link. You'll have to resolve the symbolic link with getCanonicalFile() first.

Example:

File[] files = dir.getCanonicalFile().listFiles(new myFilter());
if (files != null) {   
    for (File file : files) {
        //...

Also, it's beneficial to store the result of an "expensive" method call such as listFiles() instead of calling it repeatedly.

Comments