Skip to content

fix: avoid NPE when SCAN_PARAMETERS intent extra is missing in onCreate - #58

Open
picheli20 wants to merge 1 commit into
OutSystems:mainfrom
picheli20:fix/npe-missing-scan-parameters
Open

picheli20 wants to merge 1 commit into
OutSystems:mainfrom
picheli20:fix/npe-missing-scan-parameters

Conversation

@picheli20

Copy link
Copy Markdown

Description

OSBARCScannerActivity.onCreate reads the SCAN_PARAMETERS intent extra via IntentCompat.getSerializableExtra(...)!!. If that extra is ever absent when onCreate runs, the !! throws an unhandled NullPointerException before anything else executes, crashing the host app.

This PR replaces the unchecked !! with a null check that finishes the Activity gracefully with OSBARCError.INVALID_PARAMETERS_ERROR, mirroring how every other failure path in this Activity already reports errors (cancel, permission denied, scan error) via setResult(error.code) + finish().

Context

We're seeing this crash in production (via Crashlytics) in a consumer app using @capacitor/barcode-scanner 3.1.1 (which pins ionbarcode-android 2.1.1): low-volume, non-deterministic, no repro from our own call site (we always pass a well-formed OSBARCScanParameters). It's consistent with the OS recreating this non-root Activity (e.g. after process death while backgrounded mid-scan) without restoring the original custom Intent extra.

We can't fix this from the consumer app since the unchecked cast lives in this library. This change doesn't address why the extra can go missing (that's an Android platform-level activity-recreation edge case), but it stops it from being a hard crash and turns it into the same graceful failure path already used elsewhere in this file.

Type of changes

  • Fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Refactor (cosmetic changes)
  • Breaking change (change that would cause existing functionality to not work as expected)

Tests

Manual: confirmed via bytecode/source inspection that this is the exact crash site (matches the reported NullPointerException: null cannot be cast to non-null type com.outsystems.plugins.barcode.model.OSBARCScanParameters). No existing unit/instrumented tests cover OSBARCScannerActivity.onCreate, so none were touched; happy to add a test if you can point me at the preferred testing approach for this Activity (Compose/instrumented).

Screenshots (if appropriate)

N/A (no UI change; the missing-parameters path now finishes the Activity instead of crashing)

Checklist

  • Code follows code style of this project
  • CHANGELOG.md file is correctly updated
  • Changes require an update to the documentation
    • Documentation has been updated accordingly

🤖 Generated with Claude Code

OSBARCScannerActivity.onCreate unwrapped the SCAN_PARAMETERS
IntentCompat.getSerializableExtra result with `!!`, so any launch of
this Activity without that extra crashed with an unhandled
NullPointerException before any other code ran.

This has been observed in production consumer apps as a low-volume,
non-deterministic crash, consistent with the OS recreating this
(non-root) Activity in the background without the original custom
Intent extra (e.g. after process death while a scan was in progress).

Handle the missing-extra case the same way every other failure path
in this Activity already does: finish with
OSBARCError.INVALID_PARAMETERS_ERROR instead of crashing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@picheli20
picheli20 requested a review from a team as a code owner September 16, 2026 12:06
@picheli20
picheli20 requested review from andredestro and markemer and removed request for a team September 16, 2026 12:06
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