Unexpected NullPointerException when accessing shared preferences in Android 14 after configuration change
After trying multiple solutions online, I still can't figure this out... After trying multiple solutions online, I still can't figure this out. I'm encountering a `NullPointerException` when trying to access shared preferences in my Activity after a configuration change on Android 14. The issue seems to occur when the device orientation changes. I've set up my shared preferences like this: ```kotlin val sharedPreferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE) val myValue = sharedPreferences.getString("my_key", null) ``` When the orientation changes, the `myValue` ends up being `null`, and I get a crash with the following trace: ``` java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.myapp.MyActivity.myValue' on a null object reference ``` I've tried adding `onSaveInstanceState` to retain the value during the configuration change, but it seems that shared preferences should not need that because they are supposed to persist across configuration changes. Here's my current implementation of `onSaveInstanceState`: ```kotlin override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putString("my_key", myValue) } ``` After the change, I'm attempting to retrieve the value in `onCreate` like this: ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (savedInstanceState != null) { myValue = savedInstanceState.getString("my_key", null) } else { myValue = sharedPreferences.getString("my_key", null) } } ``` Despite these efforts, I still face the same `NullPointerException` when the configuration change occurs. Is there an issue with how I'm handling shared preferences, or is there something else that could be causing this behavior in Android 14? Any insights would be greatly appreciated! What am I doing wrong? Thanks in advance!