-1

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!

2
  • Caused by: java.lang.NullPointerException 01-13 22:28:42.318: E/AndroidRuntime(13776): at com.example.remotedevice.Player.getPlayList(Player.java:120) Which line, exactly, is 120? Commented Jan 13, 2014 at 21:39
  • as i said: "at if statement: if (dir.listFiles(new myFilter()).length > 0) {" (this is line 120) Commented Jan 14, 2014 at 8:45

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think the OP wants to know why it is returning null
The problem was that I gave directory "/dir" instead of "/sdcard/dir"... But I saw that after your post about "symbolic link" - thanks for help!
0

According to the documentation for the listFiles(), it "Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs."

Therefore, you will need to check if it is null and handle the error in these situations.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.