Repository: wordpress-mobile/WordPress-Android (language: kotlin)

Issue:
NullPointerException
Sentry Issue: [JETPACK-ANDROID-E2J](https://a8c.sentry.io/issues/4592008344/?referrer=github_integration)

```
NullPointerException: None
    at org.wordpress.android.ui.mysite.menu.MenuViewModel.start(MenuViewModel.kt:89)
    at org.wordpress.android.ui.mysite.menu.MenuActivity$onCreate$1$1$2.invoke(MenuActivity.kt:99)
    at org.wordpress.android.ui.mysite.menu.MenuActivity$onCreate$1$1$2.invoke(MenuActivity.kt:99)
    at org.wordpress.android.ui.mysite.menu.MenuActivity$onCreate$1$1$2.invoke(MenuActivity.kt:98)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
...
(168 additional frame(s) were not displayed)
```


Discussion hints:
From logs, I can say the crash is related to https://github.com/wordpress-mobile/WordPress-Android/pull/19244

Pull request: Fix Null Pointer Exception on getSelectedSite 
Fixes #19520 
Sentry [JETPACK-ANDROID-E2J](https://a8c.sentry.io/issues/4592008344/?referrer=github_integration)

This PR prevents a crash when siteRepository.getSelectedSite returns a null. If there is no site, we can continue.
Note: I could not reproduce this crash, so this a preventative measure

**To test:**
- Install the app
- Login and select a site
- Navigate to My Site > More
- ✅ Verify the more menu is shown
- Navigate around to different options and verify that the menu continues to respond

 
## Regression Notes
1. Potential unintended areas of impact
The app crashes when the more menu is accessed

2. What I did to test those areas of impact (or what existing automated tests I relied on)
Added a test to `MenuViewModelTest`

3. What automated tests I added (or what prevented me from doing so) N/A

PR submission checklist:
- [X] I have completed the Regression Notes.
- [X] I have considered adding accessibility improvements for my changes.
- [X] I have considered if this change warrants user-facing release notes and have added them to `RELEASE-NOTES.txt` if necessary.

UI Changes testing checklist: N/A



Test command: ./gradlew :WordPress:testWordpressVanillaDebugUnitTest --tests "org.wordpress.android.ui.mysite.menu.MenuViewModelTest"

Fix patch:
diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuActivity.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuActivity.kt
index 00b4f5e1a0d0..cf26a9e7c87c 100644
--- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuActivity.kt
+++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuActivity.kt
@@ -107,6 +107,7 @@ class MenuActivity : AppCompatActivity() {
         viewModel.navigation.observe(this) { handleNavigationAction(it.getContentIfNotHandled()) }
         viewModel.onSnackbarMessage.observe(this) { showSnackbar(it.getContentIfNotHandled()) }
         viewModel.onQuickStartMySitePrompts.observe(this) { handleActiveTutorialPrompt(it.getContentIfNotHandled()) }
+        viewModel.onSelectedSiteMissing.observe(this) { finish() }
 
         // Set the Compose callback for SnackbarSequencer
         snackbarSequencer.setComposeSnackbarCallback { item ->
diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuViewModel.kt
index 68ddc709d560..72bd0607643f 100644
--- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuViewModel.kt
+++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuViewModel.kt
@@ -75,6 +75,10 @@ class MenuViewModel @Inject constructor(
     private val _snackbar = MutableSharedFlow<SnackbarMessage>()
     val snackBar = _snackbar.asSharedFlow()
 
+
+    private val _onSelectedSiteMissing = MutableLiveData<Unit>()
+    val onSelectedSiteMissing = _onSelectedSiteMissing as LiveData<Unit>
+
     private var quickStartEvent: QuickStartEvent? = null
     private var isStarted = false
 
@@ -86,7 +90,13 @@ class MenuViewModel @Inject constructor(
         if (isStarted) {
             return
         }
-        val site = selectedSiteRepository.getSelectedSite()!!
+
+        val site = selectedSiteRepository.getSelectedSite()
+        if (site == null) {
+            _onSelectedSiteMissing.value = Unit
+            return
+        }
+
         this.quickStartEvent = quickStartEvent
         if (quickStartEvent != null) {
             quickStartRepository.setActiveTask(quickStartEvent.task, true)


Test patch:
diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mysite/menu/MenuViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mysite/menu/MenuViewModelTest.kt
index 7889c673f1b3..8a0f8f58fdb1 100644
--- a/WordPress/src/test/java/org/wordpress/android/ui/mysite/menu/MenuViewModelTest.kt
+++ b/WordPress/src/test/java/org/wordpress/android/ui/mysite/menu/MenuViewModelTest.kt
@@ -165,6 +165,20 @@ class MenuViewModelTest : BaseUnitTest() {
         }
     }
 
+    @Test
+    fun `given vm start, when selected site is null, then site is missing event is raised`() = test {
+        whenever(selectedSiteRepository.getSelectedSite()).thenReturn(null)
+
+        val observer = mutableListOf<Unit>()
+        viewModel.onSelectedSiteMissing.observeForever { event ->
+            observer.add(event)
+        }
+
+        viewModel.start()
+
+        assert(observer.isNotEmpty())
+    }
+
     private fun findBackupListItem(items: List<MenuItemState>) =
         items.filterIsInstance(MenuItemState.MenuListItem::class.java)
             .firstOrNull { it.listItemAction == ListItemAction.BACKUP }