juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
839 stars 489 forks source link

NPE @ MobileScanner.start$lambda-11$lambda-7(MobileScanner.kt:185) #457

Open RIPC opened 1 year ago

RIPC commented 1 year ago

Exception happens (and app crashes) sometimes after navigating to second screen and back to first where is mobile controller.

FATAL EXCEPTION: main

E/AndroidRuntime(26393): Process: com.example.exampleqr, PID: 26393 E/AndroidRuntime(26393): java.lang.NullPointerException E/AndroidRuntime(26393): at dev.steenbakker.mobile_scanner.MobileScanner.start$lambda-11$lambda-7(MobileScanner.kt:185) E/AndroidRuntime(26393): at dev.steenbakker.mobile_scanner.MobileScanner.$r8$lambda$BXvjMsq-TAiD3546zm0orsCWMqc(Unknown Source:0) E/AndroidRuntime(26393): at dev.steenbakker.mobile_scanner.MobileScanner$$ExternalSyntheticLambda2.onSurfaceRequested(Unknown Source:4) E/AndroidRuntime(26393): at androidx.camera.core.Preview.lambda$sendSurfaceRequest$1(Preview.java:516) E/AndroidRuntime(26393): at androidx.camera.core.Preview$$ExternalSyntheticLambda2.run(Unknown Source:4) E/AndroidRuntime(26393): at android.os.Handler.handleCallback(Handler.java:942) E/AndroidRuntime(26393): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(26393): at android.os.Looper.loopOnce(Looper.java:226) E/AndroidRuntime(26393): at android.os.Looper.loop(Looper.java:313) E/AndroidRuntime(26393): at android.app.ActivityThread.main(ActivityThread.java:8741) E/AndroidRuntime(26393): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(26393): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) E/AndroidRuntime(26393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

version: 3.0.0-beta.4

flutter doctor -v [√] Flutter (Channel stable, 3.3.8, on Microsoft Windows [Version 10.0.19044.2486], locale en-EN) • Flutter version 3.3.8 on channel stable at D:\flutter\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 52b3dc25f6 (3 months ago), 2022-11-09 12:09:26 +0800
• Engine revision 857bd6b74c
• Dart version 2.18.4
• DevTools version 2.15.0

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc3)
• Android SDK at C:\Users\A\AppData\Local\Android\sdk • Platform android-33, build-tools 33.0.0-rc3 • ANDROID_HOME = C:\Users\A\AppData\Local\Android\ • ANDROID_SDK_ROOT = C:\Users\A\AppData\Local\Android\Sdk • Java binary at: C:\Users\A\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\213.7172.25.2113.9123335\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) • All Android licenses accepted. • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin version 223.8214.16

[√] Connected device (4 available) • SM G980F (mobile) • adb-RF8N90STT9A-aHZg7Q._adb-tls-connect._tcp • android-arm64 • Android 13 (API 33) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19044.2486] • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.75 • Edge (web) • edge • web-javascript • Microsoft Edge 109.0.1518.61

[√] HTTP Host Availability • All required HTTP hosts are available

• No issues found!

PoeMcMoe commented 1 year ago

Experiencing same issue. On my device happens only if I reopen the screen with scan view fast enough.

Seems to be a race condition where 2 controllers may exist at the same time if I try to reopen the scan screen while the last one is still animating out. Forcing the users to wait a little for the animation to finish fixed my issue.

arnzuk commented 1 year ago

Hi,

Same issue here:

image
juliansteenbakker commented 1 year ago

Can you please try this again with version 3.0.0?

RIPC commented 1 year ago

Can you please try this again with version 3.0.0?

Yes, even with "startDelay": true exception still remains.

arnzuk commented 1 year ago

Hi

Same issue with 3.0.0. Must reopen issue.

image
meetpatel1920 commented 1 year ago

+1

Any Update?? @juliansteenbakker

E/AndroidRuntime(24235): FATAL EXCEPTION: main
E/AndroidRuntime(24235): Process: com.example.appID, PID: 24235
E/AndroidRuntime(24235): java.lang.NullPointerException
E/AndroidRuntime(24235):        at dev.steenbakker.mobile_scanner.MobileScanner.start$lambda-11$lambda-7(MobileScanner.kt:171)
E/AndroidRuntime(24235):        at dev.steenbakker.mobile_scanner.MobileScanner.$r8$lambda$BXvjMsq-TAiD3546zm0orsCWMqc(Unknown Source:0)
E/AndroidRuntime(24235):        at dev.steenbakker.mobile_scanner.MobileScanner$$ExternalSyntheticLambda2.onSurfaceRequested(Unknown Source:4)
E/AndroidRuntime(24235):        at androidx.camera.core.Preview.lambda$sendSurfaceRequest$1(Preview.java:516)
E/AndroidRuntime(24235):        at androidx.camera.core.Preview$$ExternalSyntheticLambda2.run(Unknown Source:4)
E/AndroidRuntime(24235):        at android.os.Handler.handleCallback(Handler.java:938)
E/AndroidRuntime(24235):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(24235):        at android.os.Looper.loopOnce(Looper.java:210)
E/AndroidRuntime(24235):        at android.os.Looper.loop(Looper.java:299)
E/AndroidRuntime(24235):        at android.app.ActivityThread.main(ActivityThread.java:8319)
E/AndroidRuntime(24235):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(24235):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
E/AndroidRuntime(24235):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
xiaozhizhong commented 1 year ago

For those who suffers from this bug: If you pop route or dispose controller in the callback of onDetect, try ignore the result if controller.isStarting. Codes like:

_onBarcodeDetect(BarcodeCapture capture) {
    if (controller.isStarting) return; // Add this line
    ...your codes
  }

The NPE is throwed in line 171 of MoblieScanner.kt when textureEntry is null. And the reason it is null is that the stop() is called before Camera was initialized.

Sometimes we re-enter the scanner page, the Camera is still initialized but the onDetectcallbacks immediatelly with the old scanned result(apparently it's a bug). If we then pop the route or dispose the controller, the controller will call stop() in Android and set the textureEntry to null. And because Camera initialization is asynchronous and takes time in Android, when Camera initialization is done and 'textureEntry' has been set to null, NPE will be thrown. Luckily, we could use controller.isStarting to know if the Camera initialization is done to ignore the old scanned results and also to prevent this error.