aclec / expo-zebra-scanner

11 stars 5 forks source link

NullPointerException when scanning code #20

Open whycody opened 6 days ago

whycody commented 6 days ago

Hi, thank you for your work on this library.

My device is: TC 27

I configured Zebra as you suggested and I have this code to read the scanned codes:

const [scanCodes, setScanCodes] = useState<string[]>([]);

  useEffect(() => {
    const listener = ExpoZebraScanner.addListener((event: any) => {
      if(event){
        const { scanData } = event;
        setScanCodes(prevScan => {
          let newScanData = scanData.replace(/(\r\n|\n|\r)/gm, "");
          const newScanCodes = [newScanData, ...(prevScan || [])];
          return newScanCodes.slice(0, 5);
        });
      }
    });

    ExpoZebraScanner.startScan();

    return () => {
      ExpoZebraScanner.stopScan();
      listener?.remove();
    };
  }, []);

But sometimes (not every time) when I scan an EAN or QR code code, I get this weird error:

java.lang.RuntimeException: Error receiving broadcast Intent { act=com.symbol.datawedge.ACTION_BARCODE_SCANNED flg=0x10 (has extras) } in expo.modules.zebrascanner.BarcodeReceiver@d874c6f
  android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1800)
  android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2)
  android.os.Handler.handleCallback(Handler.java:942)
  android.os.Handler.dispatchMessage(Handler.java:99)
  android.os.Looper.loopOnce(Looper.java:201)
  android.os.Looper.loop(Looper.java:288)
  android.app.ActivityThread.main(ActivityThread.java:7930)
  java.lang.reflect.Method.invoke(Native Method)
  com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
Caused by java.lang.NullPointerException: java.lang.NullPointerException
  expo.modules.kotlin.jni.JavaScriptModuleObject.emitEvent(Native Method)
  expo.modules.kotlin.events.KModuleEventEmitterWrapper.emitNative(KModuleEventEmitterWrapper.kt:48)
  expo.modules.kotlin.events.KModuleEventEmitterWrapper.emit(KModuleEventEmitterWrapper.kt:28)
  expo.modules.kotlin.modules.Module.sendEvent(Module.kt:31)
  expo.modules.zebrascanner.ExpoZebraScannerModule$definition$1$1$1.invoke(ExpoZebraScannerModule.kt:60)
  expo.modules.zebrascanner.ExpoZebraScannerModule$definition$1$1$1.invoke(ExpoZebraScannerModule.kt:60)
  expo.modules.zebrascanner.BarcodeReceiver.onReceive(ExpoZebraScannerModule.kt:35)
  android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1790)
  android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2)
  android.os.Handler.handleCallback(Handler.java:942)
  android.os.Handler.dispatchMessage(Handler.java:99)
  android.os.Looper.loopOnce(Looper.java:201)
  android.os.Looper.loop(Looper.java:288)
  android.app.ActivityThread.main(ActivityThread.java:7930)
  java.lang.reflect.Method.invoke(Native Method)
  com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)

Do you have any idea what could I have done wrong?

aclec commented 6 days ago

Hi, do you have the latest version installed? Update the dependencies list, as any changes to the setter could cause it to crash.

whycody commented 5 days ago

Yes I do have the latest version

Shadic78 commented 5 days ago

I tried to reproduce the issue with the provided code block but everything works fine with version 4.1.3 of this package and Expo 51.0.28.

@whycody Which Expo version are you working with?, did you enable the new arch?, how often do you get that error?