0

I am trying to make an app and for that I need to list files. I have found some useful code and tailored it a bit for my needs. This is my (simple) code:

// List all files from specific directory
    public void listFiles(){

        // Path to directory to scane
        String path = Environment.getExternalStorageDirectory().toString()+"/Download/";

        // New file-instance
        File f = new File(path);

        // Get the textview to display message
        TextView textview = (TextView) findViewById(R.id.textView);

        textview.setText(f.toString());

        // List the files
        File file[] = f.listFiles();

        for(int i = 0; i < file.length; i++){
            Log.d("Files", "Filename: " + file[i].getName());
        }
    }

I know that this is caused by file.length in my for loop, but I don't understand why I get this error since it grabs the files in the right directory and I have files within that directory...

E

dit: This is my stack trace

java.lang.NullPointerException: Attempt to get length of null array
            at com.example.filemaker.MainActivity.listFiles(MainActivity.java:79)
            at com.example.filemaker.MainActivity$1.onClick(MainActivity.java:31)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
5
  • please post stack trace Commented Aug 25, 2015 at 15:53
  • See "If this abstract pathname does not denote a directory, then this method returns null" from here. Maybe the path variable isn't set up correctly? Commented Aug 25, 2015 at 15:54
  • Does your application have READ_EXTERNAL_STORAGE permission? Commented Aug 25, 2015 at 15:59
  • Your code logs all the files in my Download directory. I commented out the code for getting the TextView though, which makes me think maybe the textview is what's returning null for you. The stack trace would be helpful to confirm this. Commented Aug 25, 2015 at 16:02
  • No, actually. I haven't set up the READ_EXTERNAL_STORAGE permission, since I thought that would be on while debugging. Commented Aug 25, 2015 at 16:02

1 Answer 1

1

So what line make a NPE ? the file.length ? It means that file is null, looking at File javadoc, we can see the following description in the listFiles method return :

Returns: 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.

It seems that your path is not valid.

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

4 Comments

Yes, I know why I get that error, but it doesn't seem logical, since I have browsed to that directory. I am displaying the location in a Textview, and it's equal to the directory I wanted
What do you have if you test f with "exists()" or "isDirectory()" ? try creating the file instance using docs.oracle.com/javase/7/docs/api/java/io/…
It says that the path does not exist...Do you know a better method to get the path? Instead of String path = Environment.getExternalStorageDirectory().toString()+"/Download";, I need to get to the main "Download" folder of the phone. Thanks for your comment!
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); found on another post here. don't forget to grant access to file storage in your manifest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.