Skip to content

Fix race condition NullPointerException in EmpowerPlantActivity (ANDROID-EH)#230

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

Fix race condition NullPointerException in EmpowerPlantActivity (ANDROID-EH)#230
cursor[bot] wants to merge 1 commit into
mainfrom
nullpointerexception-attempt-to-ltcvpn

Conversation

@cursor

@cursor cursor Bot commented Jun 24, 2026

Copy link
Copy Markdown

Summary

Fixes ANDROID-EH

This PR fixes a race condition that causes a NullPointerException when a database observer's callback fires in onResume() before onCreateOptionsMenu() has initialized the textCartItemCount view.

Root Cause

The race condition occurred due to the following sequence:

  1. textCartItemCount.setText() throws a NullPointerException because textCartItemCount is null
  2. textCartItemCount is null because the database observer callback fires before it is initialized
  3. The view is uninitialized because it is assigned in onCreateOptionsMenu()
  4. onCreateOptionsMenu() is invoked independently of the activity lifecycle
  5. The callback runs early because it is subscribed in onResume(), creating a race condition

Solution

Added a null check in the onResume() database observer callback before calling textCartItemCount.setText(). This prevents the NullPointerException when the callback fires before the view has been initialized.

Changes

  • Modified EmpowerPlantActivity.java to add a null safety check:
    • Changed the lambda callback to use a block with an if (textCartItemCount != null) guard before updating the text

Testing

The fix ensures that:

  • The app can launch EmpowerPlantActivity without crashing
  • The database observer callback is safely handled even when it fires before menu creation
  • The cart item count is updated correctly once the view is initialized
Open in Web Open in Cursor 

Fixes ANDROID-EH

Add null check in onResume() database observer callback before calling
textCartItemCount.setText(). This prevents a NullPointerException when
the callback fires before onCreateOptionsMenu() has initialized the
textCartItemCount view.

The race condition occurred because:
- onResume() subscribes to a database observer that updates textCartItemCount
- onCreateOptionsMenu() initializes textCartItemCount
- onCreateOptionsMenu() can be invoked after onResume(), creating a window
  where the callback fires with a null textCartItemCount reference
@sentry

sentry Bot commented Jun 24, 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 24, 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 (099d9e2).

Files with missing lines Patch % Lines
.../vu/android/empowerplant/EmpowerPlantActivity.java 0.00% 4 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main    #230   +/-   ##
=====================================
  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