From b77145401fcf3417e1b6e4e49c0271513ca26275 Mon Sep 17 00:00:00 2001 From: Beverly Date: Tue, 5 Jun 2018 13:05:12 -0400 Subject: [PATCH] Add null check in displayPreference Change-Id: I3afaa3f587dc5c9989d5e520f1c8a0b33e57ec7d Fixes: 109740402 Test: ZenModeStarredContactsPreferenceControllerTest.java --- .../ZenModeStarredContactsPreferenceController.java | 5 ++++- .../ZenModeStarredContactsPreferenceControllerTest.java | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java b/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java index 28475b6b5e3..10a7b561353 100644 --- a/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java +++ b/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java @@ -68,7 +68,10 @@ public class ZenModeStarredContactsPreferenceController extends public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); mPreference = screen.findPreference(KEY); - mPreference.setOnPreferenceClickListener(this); + + if (mPreference != null) { + mPreference.setOnPreferenceClickListener(this); + } } @Override diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeStarredContactsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeStarredContactsPreferenceControllerTest.java index 064c0911319..ad8e0de7193 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModeStarredContactsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModeStarredContactsPreferenceControllerTest.java @@ -190,4 +190,13 @@ public class ZenModeStarredContactsPreferenceControllerTest { assertThat(contacts.get(i)).isNotNull(); } } + + @Test + public void nullPreference_displayPreference() { + when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey())) + .thenReturn(null); + + // should not throw a null pointer + mMessagesController.displayPreference(mPreferenceScreen); + } }