• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Why is my array empty? didn't I already call it?

Benvanj

Lurker
Apr 1, 2019
8
1
Hi guys... I followed a tutorial on posting pictures in my app... So I have been tracking a problem and am not sure what is going on, Could you please tell me or show me, or even point me in the right direction...

the error points to another section but I traced it to an empty array:

so I have an Filepaths class:

Java:
public class FilePaths {

    //"storage/emulated/0"
    public String ROOT_DIR = Environment.getExternalStorageDirectory().getPath();

    public String PICTURES = ROOT_DIR + "/Pictures";
    public String CAMERA = ROOT_DIR + "/DCIM/camera";

    public String FIREBASE_IMAGE_STORAGE = "photos/users/";

}


in another Class I call am getting my file directories and file paths:

Code:
/**
* Search a directory and return a list of all **directories** contained inside
* @param directory
* @return
*/
public static ArrayList<String> getDirectoryPaths(String directory){
    ArrayList<String> pathArray = new ArrayList<>();
    File file = new File(directory);
    File[] listfiles = file.listFiles();
    for(int i = 0; i < listfiles.length; i++){
        if(listfiles[i].isDirectory()){
            pathArray.add(listfiles[i].getAbsolutePath());
        }
    }
    return pathArray;
}
/**
     * Search a directory and return a list of all **files** contained inside
     * @param directory
     * @return
     */
    public static ArrayList<String> getFilePaths(String directory){
        ArrayList<String> pathArray = new ArrayList<>();
        File file = new File(directory);
 
        File[] listfiles = file.listFiles();
        Log.d(TAG, "getFilePaths: in file search");
 
        if(listfiles != null){
            for(int i = 0; i < listfiles.length; i++) {
                if (listfiles[i].isFile()) {//the error traces back here where it is telling me the listfiles are empty??
                    pathArray.add(listfiles[i].getAbsolutePath());
                } else {
                    Log.d(TAG, "getFilePaths: listfiles array is equal to null");
                }
            }
        }
        return pathArray;
    }

Could anyone please tell me why
listfiles
are empty???
 
Last edited:
Code:
2019-06-05 23:16:17.200 13394-13394/com.benvanj.android.outstagram D/FileSearch: getFilePaths: in file search
2019-06-05 23:16:17.200 13394-13394/com.benvanj.android.outstagram D/FileSearch: getFilePaths: listfiles array is equal to null //.....this is where it searches for the files in the array
2019-06-05 23:16:17.203 13394-13394/com.benvanj.android.outstagram D/AndroidRuntime: Shutting down VM
2019-06-05 23:16:17.208 13394-13394/com.benvanj.android.outstagram E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.benvanj.android.outstagram, PID: 13394
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:437)
        at com.benvanj.android.outstagram.Share.GalleryFragment.setupGridView(GalleryFragment.java:167)
        at com.benvanj.android.outstagram.Share.GalleryFragment.access$300(GalleryFragment.java:35)
        at com.benvanj.android.outstagram.Share.GalleryFragment$3.onItemSelected(GalleryFragment.java:141)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:947)
        at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:936)
        at android.widget.AdapterView.access$300(AdapterView.java:56)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:901)
        at android.os.Handler.handleCallback(Handler.java:891)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:7539)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
 
Upvote 0
That's not making sense to me. I don't see how that loop executes if the array length is 0
Code:
        if(listfiles != null){
            for(int i = 0; i < listfiles.length; i++) {
                if (listfiles[i].isFile()) {//the error traces back here where it is telling me the listfiles are empty??
                    pathArray.add(listfiles[i].getAbsolutePath());
                } else {
                    Log.d(TAG, "getFilePaths: listfiles array is equal to null");
                }
            }

Can you use Arrays.toString(listfiles) before the loop to see what it gets populated with? I'm curious how a 0-length array is hitting even one iteration of that loop.
 
Upvote 0
Actually I think that there is a problem with the line
Code:
if (listfiles[i].isFile())
It is possible that the first `file' (listfiles[0]) is actually a directory/folder, e.g. the ones named "." (current folder) and ".." (parent folder).
You should check also
Code:
if(listfiles[i].isDirectory())
and decide what do to in this case.

I thought something like that, too, but I don't see how that would give an out of bounds error against ListFiles.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones