react-native-screens: NullPointerException - LifecycleRegistry.removeObserver

Description

We’re getting a few NPE in our app in prod.

NullPointerException
Parameter specified as non-null is null: method androidx.lifecycle.LifecycleRegistry.removeObserver, parameter observer
image

Searching for removeObserver in our entire project returns only this line in react-native-screens: https://github.com/software-mansion/react-native-screens/blob/6272e448afce5a112d1ddffc3cc146a453738be5/android/src/main/java/com/swmansion/rnscreens/LifecycleHelper.kt#L46

Steps to reproduce

We can’t seem to reproduce. It seems to be happening only on Android. Maybe on app start (we don’t see any breadcrumbs in Sentry).

Snack or a link to a repository

Couldn’t repro in snack.

Screens version

3.18.2

React Native version

0.71.8

Platforms

Android

JavaScript runtime

Hermes

Workflow

React Native (without Expo)

Architecture

Paper (Old Architecture)

Build type

Release mode

Device

Real device

Device model

Redmi Note 9 Pro Max - Android 11

Acknowledgements

Yes

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 22

Most upvoted comments

Here’s the patch:

diff --git a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/LifecycleHelper.kt b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/LifecycleHelper.kt
index 41bfc80..5925472 100644
--- a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/LifecycleHelper.kt
+++ b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/LifecycleHelper.kt
@@ -43,6 +43,9 @@ class LifecycleHelper {
     }
 
     fun <T> unregister(view: T) where T : View, T : LifecycleObserver? {
+        if (view == null) {
+            return
+        }
         mViewToLifecycleMap[view]?.removeObserver(view)
     }

I’m gonna do a patch using patch-package today and see if that fixes it. I’ll submit a PR if that helps.

fun <T> unregister(view: T) where T : View, T : LifecycleObserver? { if(view != null) { mViewToLifecycleMap[view]?.removeObserver(view) } } I’m going to check this line of code.

That was my plan for the patch

We can’t even repro in our own app. It seems to be happening intermittently for some users only.