Skip to content

Fix NullPointerException in cart badge update#232

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
nullpointerexception-attempt-to-bpr630
Draft

Fix NullPointerException in cart badge update#232
cursor[bot] wants to merge 1 commit into
mainfrom
nullpointerexception-attempt-to-bpr630

Conversation

@cursor

@cursor cursor Bot commented Jun 27, 2026

Copy link
Copy Markdown

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() on textCartItemCount at line 144. This happens due to a race condition:

  1. textCartItemCount is only initialized in onCreateOptionsMenu() (line 96)
  2. onResume() subscribes to Room's observeSelectedCount() which emits immediately
  3. The Room observer can fire before onCreateOptionsMenu() is called
  4. When this happens, textCartItemCount is still null, causing the NPE

Solution

Added a null check before calling setText() on textCartItemCount. 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

  • Modified onResume() method in EmpowerPlantActivity.java
  • Added null check: if (textCartItemCount != null) before setting the text
  • Converted lambda to block format to accommodate the conditional logic

Testing

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.

Open in Web Open in Cursor 

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/)
@sentry

sentry Bot commented Jun 27, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
Android com.example.vu.android 24.12.26 (241226) release
Android com.example.vu.android 24.12.26 (241226) debug
Android com.example.vu.android 24.12.26 (241226) release

⚙️ android Build Distribution Settings

@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (fbd7a20) to head (d76608b).

Files with missing lines Patch % Lines
.../vu/android/empowerplant/EmpowerPlantActivity.java 0.00% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant