From 1a94c46e878ad3a98573ec399e4ea40225fd1ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 7 May 2016 22:30:31 +0100 Subject: [PATCH] Fix NPE crash when clearing files in a directory This was very probably introduced with the new CleanCacheService stuff. Reported via ACRA: ANDROID_VERSION=6.0.1 APP_VERSION_NAME=0.100-alpha6 STACK_TRACE=java.lang.NullPointerException: Attempt to get length of null array at org.fdroid.fdroid.Utils.clearOldFiles(Utils.java:349) at org.fdroid.fdroid.Utils.clearOldFiles(Utils.java:351) at org.fdroid.fdroid.Utils.clearOldFiles(Utils.java:351) at org.fdroid.fdroid.CleanCacheService.onHandleIntent(CleanCacheService.java:54) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.os.HandlerThread.run(HandlerThread.java:61) There is nothing to clear if the directory could not be obtained for some reason, so this seems like a reasonable fix. Anything is better than a crash anyway. --- app/src/main/java/org/fdroid/fdroid/Utils.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/java/org/fdroid/fdroid/Utils.java b/app/src/main/java/org/fdroid/fdroid/Utils.java index 955ffa960..2e7ceb163 100644 --- a/app/src/main/java/org/fdroid/fdroid/Utils.java +++ b/app/src/main/java/org/fdroid/fdroid/Utils.java @@ -344,6 +344,9 @@ public final class Utils { * @param secondsAgo The number of seconds old that marks a file for deletion. */ public static void clearOldFiles(File dir, long secondsAgo) { + if (dir == null) { + return; + } long olderThan = System.currentTimeMillis() - (secondsAgo * 1000L); for (File f : dir.listFiles()) { if (f.isDirectory()) {