Fix NPE in SettingsProvider.

Bug: 34387347, 34396959, 34395671
Test: CTS tests passed, Manual testing passed
Change-Id: I333dfba05f9a4706af22b850b8a010cf391350fc
(cherry picked from commit ea617597c78e3a28304a85cfb3f790e1e36bca76)
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index 058e38a..a33ab16 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -1039,7 +1039,7 @@
                 name);
 
         // Lazy initialize ssaid if not yet present in ssaid table.
-        if (ssaid.isNull() || ssaid.getValue() == null) {
+        if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) {
             return mSettingsRegistry.generateSsaidLocked(getCallingPackage(), owningUserId);
         }
 
@@ -1923,11 +1923,13 @@
 
             // Read the user's key from the ssaid table.
             Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
-            if (userKeySetting.isNull() || userKeySetting.getValue() == null) {
+            if (userKeySetting == null || userKeySetting.isNull()
+                    || userKeySetting.getValue() == null) {
                 // Lazy initialize and store the user key.
                 generateUserKeyLocked(userId);
                 userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
-                if (userKeySetting.isNull() || userKeySetting.getValue() == null) {
+                if (userKeySetting == null || userKeySetting.isNull()
+                        || userKeySetting.getValue() == null) {
                     throw new IllegalStateException("User key not accessible");
                 }
             }
@@ -2165,7 +2167,7 @@
 
             SettingsState settingsState = peekSettingsStateLocked(key);
             if (settingsState == null) {
-                return settingsState.getNullSetting();
+                return null;
             }
 
             // getSettingLocked will return non-null result
@@ -2999,8 +3001,13 @@
                     // user data or first boot on a new device should use new ssaid generation.
                     if (isUpgrade) {
                         // Retrieve the legacy ssaid from the secure settings table.
-                        final String legacySsaid = getSettingLocked(SETTINGS_TYPE_SECURE, userId,
-                                Settings.Secure.ANDROID_ID).getValue();
+                        final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE,
+                                userId, Settings.Secure.ANDROID_ID);
+                        if (legacySsaidSetting == null || legacySsaidSetting.isNull()
+                                || legacySsaidSetting.getValue() == null) {
+                            throw new IllegalStateException("Legacy ssaid not accessible");
+                        }
+                        final String legacySsaid = legacySsaidSetting.getValue();
 
                         // Fill each uid with the legacy ssaid to be backwards compatible.
                         final List<PackageInfo> packages;