Skip to content

Catch NullPointerException for onRequestPermissionsResult#39715

Closed
chakrihacker wants to merge 3 commits into
react:mainfrom
chakrihacker:fix/add-null-pointer-exception-handling
Closed

Catch NullPointerException for onRequestPermissionsResult#39715
chakrihacker wants to merge 3 commits into
react:mainfrom
chakrihacker:fix/add-null-pointer-exception-handling

Conversation

@chakrihacker

@chakrihacker chakrihacker commented Sep 29, 2023

Copy link
Copy Markdown
Contributor

Summary:

On Android 13 Devices, we are seeing NullPointerException, which should be handled with this

Changelog:

[ANDROID] [FIXED] - Handle Crash for onRequestPermissionsResult

Test Plan:

On Android 13 Devices, we are seeing `NullPointerException`, which should be handled with this
@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Sep 29, 2023
@analysis-bot

analysis-bot commented Sep 29, 2023

Copy link
Copy Markdown
Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 16,902,910 -414,693
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 20,381,655 -304,057
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: bad027c
Branch: main

@javache

javache commented Sep 29, 2023

Copy link
Copy Markdown
Contributor

Generally catching a NullPointerException means you're masking an underlying error. What object is null?

@github-actions

github-actions Bot commented Sep 29, 2023

Copy link
Copy Markdown
Warnings
⚠️ One hour and a half have passed and the E2E jobs haven't finished yet.

Generated by 🚫 dangerJS against c114c0c

@NickGerleman

Copy link
Copy Markdown
Contributor

Do you have a callstack?

@chakrihacker

Copy link
Copy Markdown
Contributor Author

Here's the CallStack

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.cpt_oe_react_app/com.cpt_oe_react_app.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.facebook.react.bridge.Callback.invoke(java.lang.Object[])' on a null object reference
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4945)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4978)
       at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
       at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2402)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:210)
       at android.os.Looper.loop(Looper.java:299)
       at android.app.ActivityThread.main(ActivityThread.java:8136)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1028)
Caused by java.lang.NullPointerException: Attempt to invoke interface method 'void com.facebook.react.bridge.Callback.invoke(java.lang.Object[])' on a null object reference
       at com.facebook.react.modules.permissions.PermissionsModule.onRequestPermissionsResult(PermissionsModule.java:201)
       at com.facebook.react.ReactActivityDelegate$2.invoke(ReactActivityDelegate.java:201)
       at com.facebook.react.ReactActivityDelegate.onResume(ReactActivityDelegate.java:126)
       at com.facebook.react.ReactActivity.onResume(ReactActivity.java:58)
       at com.cpt_oe_react_app.MainActivity.onResume(MainActivity.kt:70)
       at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1564)
       at android.app.Activity.performResume(Activity.java:8674)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4934)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4978)
       at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
       at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2402)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:210)
       at android.os.Looper.loop(Looper.java:299)
       at android.app.ActivityThread.main(ActivityThread.java:8136)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1028)

I am on react native 0.72.4

@chakrihacker

Copy link
Copy Markdown
Contributor Author

grantResults, getPermissionAwareActivity() one of the argument is null

Comment on lines 175 to 176
mCallbacks.get(requestCode).invoke(grantResults, getPermissionAwareActivity());
mCallbacks.remove(requestCode);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead add a null-check here.

Suggested change
mCallbacks.get(requestCode).invoke(grantResults, getPermissionAwareActivity());
mCallbacks.remove(requestCode);
Callback callback = mCallbacks.get(requestCode);
if (callback != null) {
callback.invoke(grantResults, getPermissionAwareActivity());
mCallbacks.remove(requestCode);
} else {
// Log warning. It's unexpected we're getting a response here we didn't ask for.
}

Comment on lines +180 to +181
String message = "Unable to find callback with requestCode:" + requestCode;
FLog.w("PermissionsModule", message);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: inline message

Suggested change
String message = "Unable to find callback with requestCode:" + requestCode;
FLog.w("PermissionsModule", message);
FLog.w("PermissionsModule", "Unable to find callback with requestCode:" + requestCode);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, since this is FLog, you can do FLog.w("PermissionsModule", "Unable to find callback with requestCode %d", requestCode).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you updated

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@javache has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Oct 6, 2023
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@javache merged this pull request in 9252099.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants