Skip to content

[🐛] Bug Android[new architecture]: NullPointerException in putMetric: Attempt to invoke virtual method on a null object reference #8802

@sean5940

Description

@sean5940

Issue

I am encountering a NullPointerException on Android when using the Performance Monitoring module. The app shows an error toast with the message:

[perf/unknown] Attempt to invoke virtual method 'void com.google.firebase.perf.metrics.Trace.putMetric(java.lang.String, long)' on a null object reference

It seems that putMetric is being called on a Trace object that has not been initialized properly or is null at the time of execution.

I suspect this might be related to a race condition where the Trace object is not ready before putMetric is invoked, or initialization of the trace failed silently.

This issue is reproducible in the New Architecture (Fabric) environment.

Potential Fix Proposal:
I noticed that making the storage maps for traces instance variables instead of static variables in UniversalFirebasePerfModule.java might resolve this issue, as it ensures thread safety and proper lifecycle management per module instance.

For example, in UniversalFirebasePerfModule.java, changing:

private static SparseArray<Trace> traces = new SparseArray<>();
private static SparseArray<ScreenTrace> screenTraces = new SparseArray<>();
private static SparseArray<HttpMetric> httpMetrics = new SparseArray<>();

to:

private SparseArray<Trace> traces = new SparseArray<>();
private SparseArray<ScreenTrace> screenTraces = new SparseArray<>();
private SparseArray<HttpMetric> httpMetrics = new SparseArray<>();

could potentially fix the problem.

Steps to reproduce:

  1. Enable New Architecture (Fabric).
  2. Force quit the application and relaunch it.
  3. Initialize a custom trace using perf().newTrace('my_custom_trace').
  4. (Potentially immediately) call trace.putMetric('my_metric', 1).
  5. Observe the crash/toast on Android.

Project Files

Javascript

Click To Expand

package.json:

{
  "dependencies": {
    "@react-native-firebase/app": "21.7.1",
    "@react-native-firebase/perf": "21.7.1",
    "react-native": "0.81.5"
  }
}

Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?


Environment

Click To Expand

  • Platform that you're experiencing the issue on:
    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you're using that has this issue:
    • 21.7.1
  • Firebase module(s) you're using that has the issue:
    • Performance Monitoring
  • Are you using TypeScript?
    • Y

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions