-2

I'm using androidx.biometric:biometric:1.0.0-alpha03 in a test project and when pushing the negative button I always get the following error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence androidx.biometric.BiometricFragment.getNegativeButtonText()' on a null object reference
    at androidx.biometric.BiometricPrompt$1$1.run(BiometricPrompt.java:329)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)

I think this is a library issue which needs to be fixed as the fragment seems to be null or is there anything I'm doing wrong here?

val prompt = BiometricPrompt(activity, Executors.newSingleThreadExecutor(), object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {}
        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {}
        override fun onAuthenticationFailed() {}
})

val promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle(title)
        .setSubtitle(subtitle)
        .setDescription(description)
        .setNegativeButtonText(activity.getString(android.R.string.cancel))
        .build()

prompt.authenticate(promptInfo)
luckyhandler
  • 10,651
  • 3
  • 47
  • 64
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Martin Zeitler Apr 02 '19 at 19:06
  • 2
    no, this is definitely not a duplicate as it is related to a special library and correctly tagged... – luckyhandler Apr 02 '19 at 19:07
  • it is; a `NullPointerException` is a `NullPointerException` and everybody claims his `NPE` would be something special. The question as it is even lacks the context... are you sure `activity is FragmentActivity`? ever tried setting a regular `String` as negative text? – Martin Zeitler Apr 02 '19 at 19:09
  • 2
    @MartinZeitler yes, I am / yes, I have. But it is in no case related to the problem --> `mBiometricFragment` is null WITHIN the library when getting the negativeButtonText – luckyhandler Apr 02 '19 at 19:15

1 Answers1

0

This was reported three months ago as a bug, and it was reported as fixed two months ago. However, they have not yet released a fresh artifact.

There are some comments in that issue that suggest that "it's a timing thing", and that adding delays can help as a workaround.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    `/* For the case when onResume() is being called right after authenticate, we need to make sure that all fragment transactions have been committed. */ fragmentManager.executePendingTransactions();` ...this race condition is/was limited to a single situation. – Martin Zeitler Apr 02 '19 at 23:15
  • I fixed it by posting `prompt.authenticate(promptInfo)` 250 ms delayed – luckyhandler Apr 03 '19 at 15:26
  • I'm getting something similar, but not the same: `Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'java.lang.String android.content.Context.getString(int)' on a null object reference androidx.biometric.BiometricFragment$2$1.run (BiometricFragment.java:86)` Do you know if it's related? – neteinstein Aug 14 '19 at 14:02
  • @neteinstein: It feels similar, so it might have the same cause, but I have no way of knowing if it truly comes from the same bug. – CommonsWare Aug 15 '19 at 00:23
  • Already added it to the bugtracker, it is fixed already and will be in the next release – neteinstein Aug 15 '19 at 09:57
  • @neteinstein: I have the same issue. Did you manage to find a workaround? – Andrey Shcherbakov Oct 01 '19 at 11:04
  • Yes, I'm validating if it has fingerprint capabilities before invoking it. – neteinstein Oct 01 '19 at 14:12
  • I am facing this issue for few devices. anyone has idea when this issue comes? java.lang.NullPointerException: at android.hardware.biometrics.BiometricPrompt.lambda$sendError$0 (BiometricPrompt.java:490) at android.hardware.biometrics.-$$Lambda$BiometricPrompt$HqBGXtBUWNc-v8NoHYsj2gLfaRw.run (Unknown Source:6) – Ashok Reddy Narra Dec 29 '20 at 11:31