React-native: [Android 8.0] Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference

Created on 19 Jul 2018  路  22Comments  路  Source: facebook/react-native

Environment

Run react-native info in your terminal and paste its contents here.

Description

Describe your issue in detail. Include screenshots if needed. If this is a regression, let us know.

Reproducible Demo

Let us know how to reproduce the issue. Include a code sample, share a project, or share an app that reproduces the issue using https://snack.expo.io/. Please follow the guidelines for providing a MCVE: https://stackoverflow.com/help/mcve

java.lang.NullPointerException Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
NativeViewHierarchyManager.java:537 com.facebook.react.uimanager.NativeViewHierarchyManager.dropView
NativeViewHierarchyManager.java:538 com.facebook.react.uimanager.NativeViewHierarchyManager.dropView
NativeViewHierarchyManager.java:538 com.facebook.react.uimanager.NativeViewHierarchyManager.dropView
NativeViewHierarchyManager.java:538 com.facebook.react.uimanager.NativeViewHierarchyManager.dropView
NativeViewHierarchyManager.java:538 com.facebook.react.uimanager.NativeViewHierarchyManager.dropView
NativeViewHierarchyManager.java:431 com.facebook.react.uimanager.NativeViewHierarchyManager.manageChildren
UIViewOperationQueue.java:180 com.facebook.react.uimanager.UIViewOperationQueue$ManageChildrenOperation.execute
UIViewOperationQueue.java:819 com.facebook.react.uimanager.UIViewOperationQueue$1.run
UIViewOperationQueue.java:926 com.facebook.react.uimanager.UIViewOperationQueue.flushPendingBatches
UIViewOperationQueue.java:47 com.facebook.react.uimanager.UIViewOperationQueue.access$2100
UIViewOperationQueue.java:986 com.facebook.react.uimanager.UIViewOperationQueue$DispatchUIFrameCallback.doFrameGuarded
GuardedFrameCallback.java:31 com.facebook.react.uimanager.GuardedFrameCallback.doFrame
ReactChoreographer.java:136 com.facebook.react.modules.core.ReactChoreographer$ReactChoreographerDispatcher.doFrame
ChoreographerCompat.java:107 com.facebook.react.modules.core.ChoreographerCompat$FrameCallback$1.doFrame
Choreographer.java:964 android.view.Choreographer$CallbackRecord.run
Choreographer.java:778 android.view.Choreographer.doCallbacks
Choreographer.java:710 android.view.Choreographer.doFrame
Choreographer.java:952 android.view.Choreographer$FrameDisplayEventReceiver.run
Handler.java:789 android.os.Handler.handleCallback
Handler.java:98 android.os.Handler.dispatchMessage
Looper.java:172 android.os.Looper.loop
ActivityThread.java:6637 android.app.ActivityThread.main
Method.java:-2 java.lang.reflect.Method.invoke
Zygote.java:240 com.android.internal.os.Zygote$MethodAndArgsCaller.run
ZygoteInit.java:767 com.android.internal.os.ZygoteInit.main

Android Locked

Most helpful comment

@patrickkempff , we have the same issue, here is our env info:

Environment:
  OS: macOS High Sierra 10.13.4
  Node: 8.8.1
  Yarn: 1.7.0
  npm: 5.4.2
  Watchman: 4.7.0
  Xcode: Xcode 9.4.1 Build version 9F2000
  Android Studio: 3.1 AI-173.4819257

Packages: (wanted => installed)
  react: ^16.3.2 => 16.4.1
  react-native: 0.55.4 => 0.55.4

It seems that here:
[email protected]

534:      for (int i = viewGroupManager.getChildCount(viewGroup) - 1; i >= 0; i--) {
535:        View child = viewGroupManager.getChildAt(viewGroup, i);
536:        if (mTagsToViews.get(child.getId()) != null) {
537:          dropView(child);
538:        }
539:      }

In master new lines positions, above code starting from line 571:

child is null sometimes and child.getId() throws exception.

First I thought that something is not properly synchronized, but couldn't find the place where it's not.

Quick fix PR would be to do check child != null, and it should resolve it as if there is no child, we don't care, going forward.

And btw. right now it's our top crash.

All 22 comments

Only got this on Oreo

@Yusiro85 Thank you for reaching out. To investigate this further it would be really helpful to provide as much information as possible.

To prevent this issue from being closed, please make sure to add information about your environment using react-native info and paste its contents in the issue.

To increase the probability of this issue getting fixed, it would be really helpful to provide a reproducible demo.

Can confirm the problem is still a thing on latest (0.56.0) react native version.
react-native info output

React Native Environment Info:
    System:
      OS: macOS Sierra 10.12.6
      CPU: x64 Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
      Memory: 37.20 MB / 8.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 9.10.1 - ~/.nvm/versions/node/v9.10.1/bin/node
      npm: 6.1.0 - ~/.nvm/versions/node/v9.10.1/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 11.2, macOS 10.13, tvOS 11.2, watchOS 4.2
      Android SDK:
        Build Tools: 21.1.2, 22.0.1, 23.0.1, 23.0.2, 23.0.3, 24.0.0, 24.0.1, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.0, 26.0.1, 26.0.2
        API Levels: 19, 21, 22, 23, 24, 25, 26
    IDEs:
      Android Studio: 3.1 AI-173.4819257
      Xcode: 9.2/9C40b - /usr/bin/xcodebuild
    npmPackages:
      react: 16.4.1 => 16.4.1 
      react-native: 0.56.0 => 0.56.0

@patrickkempff , we have the same issue, here is our env info:

Environment:
  OS: macOS High Sierra 10.13.4
  Node: 8.8.1
  Yarn: 1.7.0
  npm: 5.4.2
  Watchman: 4.7.0
  Xcode: Xcode 9.4.1 Build version 9F2000
  Android Studio: 3.1 AI-173.4819257

Packages: (wanted => installed)
  react: ^16.3.2 => 16.4.1
  react-native: 0.55.4 => 0.55.4

It seems that here:
[email protected]

534:      for (int i = viewGroupManager.getChildCount(viewGroup) - 1; i >= 0; i--) {
535:        View child = viewGroupManager.getChildAt(viewGroup, i);
536:        if (mTagsToViews.get(child.getId()) != null) {
537:          dropView(child);
538:        }
539:      }

In master new lines positions, above code starting from line 571:

child is null sometimes and child.getId() throws exception.

First I thought that something is not properly synchronized, but couldn't find the place where it's not.

Quick fix PR would be to do check child != null, and it should resolve it as if there is no child, we don't care, going forward.

And btw. right now it's our top crash.

@chrusart thank you for your response. I am not sure but somehow I have a feeling #17178 could be related. Can you try the steps in https://github.com/facebook/react-native/issues/17178#issuecomment-394662148 and check if this resolves the issue your are having? Thank you.

@patrickkempff sure, will check it, but anyway we cannot assume that somewhere else some view will not be removed not synchronized and returned as null, and this check will not harm. UIImplementation synchronization could be not the only place we know that cause it.

@patrickkempff sorry, but not sure if I can check it as it's really hard to reproduce and crash is in different place. Of course I can check if crashes described in #17178 happen or not after fix, but I think I'm not able to get crash from this issue reproduced, or be sure it's fixed with UIImplementation synchronizations.

PR created from personal account.

@chrusart thread safety in UIImplementation operations doesn't solve this issue unfortunately, we're still encountering this error after fixing crashes described in #17178.

BTW all of our crashes are on Oreo as well.

Thanks @krszwsk , our as well.

Hi all,
Just bumped to this issue while developing native view.
Looks like this is a bug in custom view frameworks you can use. In android most common way to reproduce it is ViewManager's incorrect child managing methods usage:

public View getChildAt(T parent, int index) 
public void removeViewAt(T parent, int index) 

So, be careful with managing your native view children

Is this happening in newer versions of React Native? We are using v0.52 at the moment and the crashes are only increasing over time. All on Android 8.

@kashishgrover Yes, the problem persists in the latest RN version (0.56.0).

@8of thanks for the reply. I hope this gets fixed soon 馃槙

So there is no work around for this crash?

i'm getting this problem too, currently using react 16.4.1 and react-native 0.56.0 and only happened in Android Oreo

This error still happened here in Android Oreo.

I'm seeing it in my app on Android Oreo devices.

Environment:
  OS: macOS High Sierra 10.13.6
  Node: 10.9.0
  Yarn: 1.9.4
  npm: 6.4.1
  Watchman: 4.9.0
  Xcode: Xcode 10.1 Build version 10B61
  Android Studio: 3.2 AI-181.5540.7.32.5056338

Packages: (wanted => installed)
  react: 16.0.0 => 16.0.0
  react-native: ^0.51.0 => 0.51.1

We're having the same issue, the issue wasn't present until we pushed a new Code-Push update, of which most notable changes were new requests fetching images from url. No package updates whatsoever.

Crashes were only present on Modal dismisses, and not reproducable in Dev environment. Crashes disappeared when the app was reinstalled on one device where the crashes were observed.

  React Native Environment Info:
    System:
      OS: macOS 10.14
      CPU: x64 Intel(R) Core(TM) i5-4570R CPU @ 2.70GHz
      Memory: 457.23 MB / 16.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 8.5.0 - /usr/local/bin/node
      Yarn: 1.5.1 - /usr/local/bin/yarn
      npm: 6.4.1 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 5.0
      Android SDK:
        Build Tools: 23.0.1, 23.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.0, 26.0.1, 26.0.2, 26.0.3, 27.0.3
        API Levels: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
    IDEs:
      Android Studio: 3.0 AI-171.4443003
      Xcode: 10.0/10A255 - /usr/bin/xcodebuild
    npmPackages:
      react: ^16.4.1 => 16.4.2
      react-native: ^0.56.0 => 0.56.0
    npmGlobalPackages:
      react-native-cli: 2.0.1

We can and will upgrade to RN.57 but until then we can't rollback until we can reproduce the problem.

We're having the same issue, the issue wasn't present until we pushed a new Code-Push update

I believe I'm seeing the same thing; this issue appears to start only after a CodePush update. This is the top cause of crashes for our app.

Our customers also face this issue on Android 8.0.0 devices, not on 8.1.0.
We're using react-native 0.56.0

Was this page helpful?
0 / 5 - 0 ratings