Skip to content

bug:NullPointerException on BarcodeScanner.startScan after updating to ionic 8/plugin v8 #324

@mojontwins

Description

@mojontwins

Plugin(s)

  • Barcode Scanning
  • Face Detection
  • Face Mesh Detection
  • Selfie Segmentation
  • Subject Segmentation
  • Translation

Version

8.0.1

Platform(s)

  • Android
  • iOS
  • Web

Current behavior

I've been maintaining a ionic-capacitor based app that uses MLkit Barcode Scanner. I've recently updated the app to Ionic 8, and the plugin to v8.0.1, and now I get a NullPointerException when I try to start a new scan.

  E  Attempt to invoke virtual method 'void androidx.camera.view.PreviewView.setScaleType(androidx.camera.view.PreviewView$ScaleType)' on a null object reference
     java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.camera.view.PreviewView.setScaleType(androidx.camera.view.PreviewView$ScaleType)' on a null object reference
     	at io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.BarcodeScanner.lambda$startScan$0(BarcodeScanner.java:106)
     	at io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.BarcodeScanner.$r8$lambda$zbIUX_mdGY-ehC6dJnja9lN0-xQ(Unknown Source:0)
     	at io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.BarcodeScanner$$ExternalSyntheticLambda6.run(D8$$SyntheticClass:0)
     	at android.os.Handler.handleCallback(Handler.java:907)
     	at android.os.Handler.dispatchMessage(Handler.java:105)
     	at android.os.Looper.loop(Looper.java:216)
     	at android.app.ActivityThread.main(ActivityThread.java:7625)
     	at java.lang.reflect.Method.invoke(Native Method)
     	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
     	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

This is the relevant code

  async scanSingleBarcode() {
    return new Promise<Barcode> (async resolve => {
      document.querySelector('body')?.classList.add('barcode-scanner-active');

      const listener = await BarcodeScanner.addListener(
        'barcodesScanned',
        async result => {
          await listener.remove();
          document
            .querySelector('body')?.classList.remove('barcode-scanner-active');
          await BarcodeScanner.stopScan();
          resolve(result.barcodes[0]);
        },
      );

      await BarcodeScanner.startScan();
    });
  };

BTW the example in README.md seems outdated as the listener should be barcodesScanned as opposed to barcodeScanned.

Expected behavior

Camera opens and scans a QR, no NullPointerException

Reproduction

https://github.com/mojontwins/minimalcam

Steps to reproduce

  • ionic capacitor build android,
  • launch from Android Studio and run on device,
  • Press "Scan QR" button.

Other information

No response

Capacitor doctor

$ npx cap doctor
   Capacitor Doctor

Latest Dependencies:

  @capacitor/cli: 8.2.0
  @capacitor/core: 8.2.0
  @capacitor/android: 8.2.0
  @capacitor/ios: 8.2.0

Installed Dependencies:

  @capacitor/android: 8.2.0
  @capacitor/ios: 8.2.0
  @capacitor/cli: 8.2.0
  @capacitor/core: 8.2.0

[success] Android looking great! �
[error] Xcode is not installed

Before submitting

  • I have read and followed the bug report guidelines.
  • I have attached links to possibly related issues and discussions.
  • I understand that incomplete issues (e.g. without reproduction) are closed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions