Fix NullPointerException in cart badge update#232
Draft
cursor[bot] wants to merge 1 commit into
Draft
Conversation
Add null check before calling setText() on textCartItemCount to prevent NPE when Room observer emits before onCreateOptionsMenu() initializes the view. The race condition occurred because: - onResume() subscribes to Room's observeSelectedCount() which emits immediately - textCartItemCount is only initialized in onCreateOptionsMenu() - onCreateOptionsMenu() may not be called before the observer fires Fixes [ANDROID-HZ](https://demo.sentry.io/issues/7562174288/)
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #232 +/- ##
=====================================
Coverage 0.00% 0.00%
=====================================
Files 16 16
Lines 875 877 +2
Branches 65 65
=====================================
- Misses 875 877 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes ANDROID-HZ
This PR addresses a NullPointerException that occurs when updating the cart badge count in
EmpowerPlantActivity.Problem
The app crashes with a NPE when calling
setText()ontextCartItemCountat line 144. This happens due to a race condition:textCartItemCountis only initialized inonCreateOptionsMenu()(line 96)onResume()subscribes to Room'sobserveSelectedCount()which emits immediatelyonCreateOptionsMenu()is calledtextCartItemCountis still null, causing the NPESolution
Added a null check before calling
setText()ontextCartItemCount. This defensive programming approach ensures that if the view hasn't been initialized yet, we simply skip the update. The badge will be updated once the view is available and the observer emits again.Changes
onResume()method inEmpowerPlantActivity.javaif (textCartItemCount != null)before setting the textTesting
The fix prevents the NPE while maintaining the same functionality. When the cart badge view is available, it will be updated correctly with the item count.