1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Benvanj, Jun 5, 2019.

  1. Benvanj

    Benvanj Lurker
    Thread Starter

    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:

    Code (Java):
    1. public class FilePaths {
    2.  
    3.     //"storage/emulated/0"
    4.     public String ROOT_DIR = Environment.getExternalStorageDirectory().getPath();
    5.  
    6.     public String PICTURES = ROOT_DIR + "/Pictures";
    7.     public String CAMERA = ROOT_DIR + "/DCIM/camera";
    8.  
    9.     public String FIREBASE_IMAGE_STORAGE = "photos/users/";
    10.  
    11. }

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

    Code (Text):
    1.  
    2. /**
    3. * Search a directory and return a list of all **directories** contained inside
    4. * @param directory
    5. * @return
    6. */
    7. public static ArrayList<String> getDirectoryPaths(String directory){
    8.     ArrayList<String> pathArray = new ArrayList<>();
    9.     File file = new File(directory);
    10.     File[] listfiles = file.listFiles();
    11.     for(int i = 0; i < listfiles.length; i++){
    12.         if(listfiles[i].isDirectory()){
    13.             pathArray.add(listfiles[i].getAbsolutePath());
    14.         }
    15.     }
    16.     return pathArray;
    17. }
    18. /**
    19.      * Search a directory and return a list of all **files** contained inside
    20.      * @param directory
    21.      * @return
    22.      */
    23.     public static ArrayList<String> getFilePaths(String directory){
    24.         ArrayList<String> pathArray = new ArrayList<>();
    25.         File file = new File(directory);
    26.  
    27.         File[] listfiles = file.listFiles();
    28.         Log.d(TAG, "getFilePaths: in file search");
    29.  
    30.         if(listfiles != null){
    31.             for(int i = 0; i < listfiles.length; i++) {
    32.                 if (listfiles[i].isFile()) {//the error traces back here where it is telling me the listfiles are empty??
    33.                     pathArray.add(listfiles[i].getAbsolutePath());
    34.                 } else {
    35.                     Log.d(TAG, "getFilePaths: listfiles array is equal to null");
    36.                 }
    37.             }
    38.         }
    39.         return pathArray;
    40.     }
    Could anyone please tell me why
    are empty???
     


    #1 Benvanj, Jun 5, 2019
    Last edited: Jun 5, 2019
  2. 23tony

    23tony Well-Known Member

    Can you share the exact text of the error? Maybe a stack trace?
     
  3. Benvanj

    Benvanj Lurker
    Thread Starter

    Code (Text):
    1. 2019-06-05 23:16:17.200 13394-13394/com.benvanj.android.outstagram D/FileSearch: getFilePaths: in file search
    2. 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
    3. 2019-06-05 23:16:17.203 13394-13394/com.benvanj.android.outstagram D/AndroidRuntime: Shutting down VM
    4. 2019-06-05 23:16:17.208 13394-13394/com.benvanj.android.outstagram E/AndroidRuntime: FATAL EXCEPTION: main
    5.     Process: com.benvanj.android.outstagram, PID: 13394
    6.     java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    7.         at java.util.ArrayList.get(ArrayList.java:437)
    8.         at com.benvanj.android.outstagram.Share.GalleryFragment.setupGridView(GalleryFragment.java:167)
    9.         at com.benvanj.android.outstagram.Share.GalleryFragment.access$300(GalleryFragment.java:35)
    10.         at com.benvanj.android.outstagram.Share.GalleryFragment$3.onItemSelected(GalleryFragment.java:141)
    11.         at android.widget.AdapterView.fireOnSelected(AdapterView.java:947)
    12.         at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:936)
    13.         at android.widget.AdapterView.access$300(AdapterView.java:56)
    14.         at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:901)
    15.         at android.os.Handler.handleCallback(Handler.java:891)
    16.         at android.os.Handler.dispatchMessage(Handler.java:102)
    17.         at android.os.Looper.loop(Looper.java:207)
    18.         at android.app.ActivityThread.main(ActivityThread.java:7539)
    19.         at java.lang.reflect.Method.invoke(Native Method)
    20.         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    21.         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
     
  4. 23tony

    23tony Well-Known Member

    That's not making sense to me. I don't see how that loop executes if the array length is 0
    Code (Text):
    1.         if(listfiles != null){
    2.             for(int i = 0; i < listfiles.length; i++) {
    3.                 if (listfiles[i].isFile()) {//the error traces back here where it is telling me the listfiles are empty??
    4.                     pathArray.add(listfiles[i].getAbsolutePath());
    5.                 } else {
    6.                     Log.d(TAG, "getFilePaths: listfiles array is equal to null");
    7.                 }
    8.             }
    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.
     
  5. marcelpreda

    marcelpreda Member

    Actually I think that there is a problem with the line
    Code (Text):
    1. 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 (Text):
    1. if(listfiles[i].isDirectory())
    and decide what do to in this case.
     
  6. 23tony

    23tony Well-Known Member

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

Share This Page

Loading...