From a9c73dd278d4a4d52501b27c65e33f56fd246cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 5 May 2016 12:05:42 +0100 Subject: [PATCH] Avoid NPE in getNotificationTitle See the following crash reported via ACRA: java.lang.NullPointerException at org.fdroid.fdroid.net.DownloaderService.getNotificationTitle(DownloaderService.java:188) at org.fdroid.fdroid.net.DownloaderService.createNotification(DownloaderService.java:167) at org.fdroid.fdroid.net.DownloaderService.handleIntent(DownloaderService.java:274) at org.fdroid.fdroid.net.DownloaderService$ServiceHandler.handleMessage(DownloaderService.java:107) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.os.HandlerThread.run(HandlerThread.java:61) I'm not sure what the source of the null App was (could be that we couldn't access the DB for some reason, or perhaps that somehow the app wasn't in it anymore). In any case, it doesn't hurt to double check here. If the app is null, simply fall back to the title without the app name. --- .../java/org/fdroid/fdroid/net/DownloaderService.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java index f29b880e8..d483d36e4 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java @@ -181,15 +181,14 @@ public class DownloaderService extends Service { * message. */ private String getNotificationTitle(@Nullable String packageName) { - String title; if (packageName != null) { - App app = AppProvider.Helper.findByPackageName( + final App app = AppProvider.Helper.findByPackageName( getContentResolver(), packageName, new String[]{AppProvider.DataColumns.NAME}); - title = getString(R.string.downloading_apk, app.name); - } else { - title = getString(R.string.downloading); + if (app != null) { + return getString(R.string.downloading_apk, app.name); + } } - return title; + return getString(R.string.downloading); } public static PendingIntent createAppDetailsIntent(@NonNull Context context, int requestCode, @Nullable String packageName) {