0

I was having some trouble when trying to read zip file from Android external SD card.

Here is my code:

File[] fileArr = mVersionUpgradeViewModel.getZipFileFromSDCard();
for(int i = 0; i < fileArr.length; i++){
    System.out.println(fileArr[i]);
}

public File[] getZipFileFromSDCard() {
    File[] matchingFiles = new File[0];
    File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    if(f.exists()) {
        matchingFiles = f.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith("zip");
            }
        });
    }
    return matchingFiles;
}

And my emulator folder structure:

enter image description here

The zip folder is there. But with the code above, I am getting error message like:

Caused by: java.lang.NullPointerException: Attempt to get length of null array
    at com.mainapp.asynctask.VersionUpgradeTask.doInBackground(VersionUpgradeTask.java:86)
    at com.mainapp.asynctask.VersionUpgradeTask.doInBackground(VersionUpgradeTask.java:15)
    at android.os.AsyncTask$2.call(AsyncTask.java:333)

The error is pointing to the first for loop. It was working last time. But after I tried the newer version installation code, the method above breaks. Any ideas?

Thanks!

  • `NullPointerException` usually indicates problem with variables and/or prarameters that your code works with. Effective resolution calls for debugging the code. – soufrk Jun 11 '18 at 07:29
  • But then the zip file is in the sd card. Do you have any ideas why is it so? –  Jun 11 '18 at 07:32
  • I don't think it's duplicate. Mine is trying to read from external sd card but it returns null although the file is there –  Jun 11 '18 at 07:35
  • @guest176969 Have you check that "mVersionUpgradeViewModel" is not null? and what is exectly "mVersionUpgradeViewModel" this class. – Mayur Patel Jun 11 '18 at 07:50
  • `File.listFiles()` returns null under conditions which are clearly defined in the Javadoc. You aren't testing for it. Do so. – user207421 Jun 12 '18 at 01:09

2 Answers2

0

Are you sure you given permission of read external storage

List<File> fileArr = getZipFileFromSDCard();
    for (int i = 0; i < fileArr.size(); i++) {
        System.out.println(fileArr.get(i));
    }

 public List<File> getZipFileFromSDCard() {
    List<File> matchingFiles = new ArrayList<>();
    File wallpaperDirectory = new File(Environment.getExternalStorageDirectory().toString());
    File[] files = wallpaperDirectory.listFiles();
    for (File file : files) {
        if (file.getName().endsWith("pdf")) {
            matchingFiles.add(file);
        }
    }

    return matchingFiles;
}
Gautam Surani
  • 1,136
  • 10
  • 21
0

You initialize matchingFiles with an array of 0 elements:

File[] matchingFiles = new File[0];

and then you check whether Environment.DIRECTORY_DOWNLOADS exists and if so, you recreate matchingFiles to be an array of the files inside it ending with zip.

The stack trace clearly shows that the error is not thrown from this method, but it is safe to assume that the result of the method is problematic.

Now, matchingFiles is a null array later, so Environment.DIRECTORY_DOWNLOADS exists indeed, so you either do not have reading permission of the folder, or Environment.DIRECTORY_DOWNLOADS was not set to be the Download folder in your structure shown, or the zip file you are checking is restricted from the user running the application.

You may want to check whether matchingFiles is null and if so, set it to be new File[0] at the end of the method to improve your code, but the solution is clearly to check your permissions to the folder and the file.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175