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

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

```
NullPointerException: None
    at org.wordpress.android.ui.posts.EditPostRepository.getDateCreated(EditPostRepository.kt:86)
    at org.wordpress.android.ui.posts.PublishSettingsViewModel.getCurrentPublishDateAsCalendar(PublishSettingsViewModel.kt:209)
    at org.wordpress.android.ui.posts.PublishSettingsViewModel.start(PublishSettingsViewModel.kt:74)
    at org.wordpress.android.ui.posts.prepublishing.publishsettings.PrepublishingPublishSettingsViewModel.start(PrepublishingPublishSettingsViewModel.kt:38)
    at org.wordpress.android.ui.posts.PublishSettingsFragment.onCreateView(PublishSettingsFragment.kt:75)
...
(15 additional frame(s) were not displayed)
```


Discussion hints:
Thanks for reporting! 👍 <!-- generated_by_dangermattic -->


Pull request: Fix: Null Pointer Exception in PublishSettingsViewModel
Fixes #20272 

This PR fixes a NPE when accessing `EditPostRepository.dateCreated`. The getter for `dateCreated` uses a `post!!` which will throw a NPE if the post is null. This PR replaces the use of `let` with `takeIf { it.hasPost() } `to ensure we don't call dateCreated when there is no post.

-----

## To Test:
I was unable to recreate the issue, but did write a unit test to validate the new `takeIf` logic.
Please verify that all unit tests pass CI

-----

## Regression Notes

1. Potential unintended areas of impact
Continue to experience the NPE crash

2. What I did to test those areas of impact (or what existing automated tests I relied on)
Add unit test to `EditPostPublishSettingsViewModelTest`

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.

-----

## Testing Checklist (strike-out the not-applying and unnecessary ones): N/A


Test command: ./gradlew :WordPress:testWordpressVanillaDebugUnitTest --tests "org.wordpress.android.ui.posts.EditPostPublishSettingsViewModelTest"

Fix patch:
diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PublishSettingsViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PublishSettingsViewModel.kt
index 6b9ac3478a36..0c3a87edcbef 100644
--- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PublishSettingsViewModel.kt
+++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PublishSettingsViewModel.kt
@@ -71,8 +71,9 @@ constructor(
 
     open fun start(postRepository: EditPostRepository?) {
         editPostRepository = postRepository
-        val startCalendar = postRepository?.let { getCurrentPublishDateAsCalendar(it) }
-            ?: localeManagerWrapper.getCurrentCalendar()
+        val startCalendar = postRepository?.takeIf { it.hasPost() }?.let {
+            getCurrentPublishDateAsCalendar(it)
+        } ?: localeManagerWrapper.getCurrentCalendar()
         updateDateAndTimeFromCalendar(startCalendar)
         onPostStatusChanged(postRepository?.getPost())
     }


Test patch:
diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/EditPostPublishSettingsViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/EditPostPublishSettingsViewModelTest.kt
index ff79f33bbd8f..ecd6b449465b 100644
--- a/WordPress/src/test/java/org/wordpress/android/ui/posts/EditPostPublishSettingsViewModelTest.kt
+++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/EditPostPublishSettingsViewModelTest.kt
@@ -95,6 +95,7 @@ class EditPostPublishSettingsViewModelTest : BaseUnitTest() {
             null
         }
         whenever(editPostRepository.getPost()).thenReturn(post)
+        whenever(editPostRepository.hasPost()).thenReturn(true)
     }
 
     @Test
@@ -450,4 +451,25 @@ class EditPostPublishSettingsViewModelTest : BaseUnitTest() {
 
         assertThat(schedulingReminderPeriod).isEqualTo(ONE_HOUR)
     }
+
+    @Test
+    fun `on start sets current date when post not present in the repository`() {
+        var uiModel: PublishUiModel? = null
+        viewModel.onUiModel.observeForever {
+            uiModel = it
+        }
+
+        whenever(editPostRepository.hasPost()).thenReturn(false)
+        whenever(editPostRepository.getPost()).thenReturn(null)
+
+        viewModel.start(editPostRepository)
+
+        assertThat(viewModel.year).isEqualTo(2019)
+        assertThat(viewModel.month).isEqualTo(6)
+        assertThat(viewModel.day).isEqualTo(6)
+        assertThat(viewModel.hour).isEqualTo(10)
+        assertThat(viewModel.minute).isEqualTo(20)
+
+        assertThat(uiModel!!.publishDateLabel).isEqualTo("Immediately")
+    }
 }