braze-react-native-sdk
braze-react-native-sdk copied to clipboard
[Bug]: BrazeReactBridge.setCustomUserAttributeObject NullPointerException
Which Platforms?
Android
Which React Native Version?
0.68.7
Which @braze/react-native-sdk SDK version?
8.3.0
Repro Rate
100%
Steps To Reproduce
-
import Braze from "@braze/react-native-sdk". -
Use Braze.setCustomUserAttribute :
Braze.setCustomUserAttribute( 'xxx', attribution['xxx'] || null, );
Expected Behavior
Still working when value(parameter of "setCustomUserAttribute" method) is null.
Actual Incorrect Behavior
There is "NullPointerException error" when starting app in the first time.
Verbose Logs
Exception java.lang.NullPointerException: Parameter specified as non-null is null: method com.braze.reactbridge.BrazeReactBridge.setCustomUserAttributeObject, parameter value
at com.braze.reactbridge.BrazeReactBridge.setCustomUserAttributeObject
at java.lang.reflect.Method.invoke
at com.facebook.react.bridge.JavaMethodWrapper.invoke (JavaMethodWrapper.java:372)
at com.facebook.react.bridge.JavaModuleWrapper.invoke (JavaModuleWrapper.java:188)
at com.facebook.react.bridge.queue.NativeRunnable.run
at android.os.Handler.handleCallback (Handler.java:958)
at android.os.Handler.dispatchMessage (Handler.java:99)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage (MessageQueueThreadHandler.java:27)
at android.os.Looper.loopOnce (Looper.java:230)
at android.os.Looper.loop (Looper.java:319)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run (MessageQueueThreadImpl.java:228)
at java.lang.Thread.run (Thread.java:1012)
Additional Information
I encountered a NullPointerException when using the Braze.setAttributionData method to set a listener. I found that there is an issue occurring when the value is null within the if/else condition.
In my usage code:
@braze/react-native-sdk/src/braze.js
@braze/react-native-sdk/android/main/java/com/braze/reactbridge/BrazeReactBridgeImpl.kt
And here is my opinion to fix this situation:
fun setCustomUserAttributeObject(key: String?, value: ReadableMap?, merge: Boolean, callback: Callback?) {
if (key == null) {
brazelog { "Key was null. Not logging setCustomUserAttributeObject." }
return
}
/* Need null case condition for value like below */
if (value == null) {
brazelog { "Value was null. Not logging setCustomUserAttributeObject." }
return
}
val json = JSONObject(parseReadableMap(value))
runOnUser {
callback.reportResult(it.setCustomAttribute(key, json, merge))
}
}
Another solution for this problem:
Of course, we could change the usage code to fix this issue instantly by replacing null with an empty array '[ ]'. However, I still tried to identify the root cause of the problem, and I wrote this bug report in the hope that it would be helpful to you :)
Hey @owonie, thanks for raising this and sharing some detailed reproduction steps. We've filed a ticket to look into this and will keep you updated on a fix.
In the meantime, it sounds like you have a valid workaround with the empty array. Would that suffice for your use case at the moment?
Thanks for your quickly reply. It's sufficient for my case, I will wait for your update. :)
Thanks @owonie for your contribution! This fix was released in React Native SDK version 9.2.0.
I will be closing this issue now, but feel free to re-open if you have any issues regarding this. Thank you!