From b69041bea2c86722ab4a213be3d506d0dc330ec0 Mon Sep 17 00:00:00 2001 From: eguven Date: Fri, 15 Feb 2019 12:39:51 +0000 Subject: [PATCH] Prevent NullPointerException exceptions in SimpleCache initialization This is a temporary fix to prevent NullPointerException exceptions. Though writing to cache will still fail if the cache folder isn't a real folder. There are a few thing we can try: * The listing might be failing because the cache folder is just created. We can wait and try again. * If the cache folder is a regular file, we can try deleting and creating a folder with the same name. PiperOrigin-RevId: 234121925 --- .../upstream/cache/SimpleCache.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java index bf1c0a8918..c5956aa0ed 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java @@ -582,15 +582,17 @@ public final class SimpleCache implements Cache { * @throws IOException If there is an error loading or generating the UID. */ private static long loadUid(File directory, File[] files) throws IOException { - for (File file : files) { - String fileName = file.getName(); - if (fileName.endsWith(UID_FILE_SUFFIX)) { - try { - return parseUid(fileName); - } catch (NumberFormatException e) { - // This should never happen, but if it does delete the malformed UID file and continue. - Log.e(TAG, "Malformed UID file: " + file); - file.delete(); + if (files != null) { + for (File file : files) { + String fileName = file.getName(); + if (fileName.endsWith(UID_FILE_SUFFIX)) { + try { + return parseUid(fileName); + } catch (NumberFormatException e) { + // This should never happen, but if it does delete the malformed UID file and continue. + Log.e(TAG, "Malformed UID file: " + file); + file.delete(); + } } } }