-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Description
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:
- Enable New Architecture (Fabric).
- Force quit the application and relaunch it.
- Initialize a custom trace using
perf().newTrace('my_custom_trace'). - (Potentially immediately) call
trace.putMetric('my_metric', 1). - 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.settingsjetifier=truefor 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-firebaseversion you're using that has this issue:21.7.1
Firebasemodule(s) you're using that has the issue:Performance Monitoring
- Are you using
TypeScript?Y