braze-react-native-sdk icon indicating copy to clipboard operation
braze-react-native-sdk copied to clipboard

[Bug]: BrazeReactBridge.setCustomUserAttributeObject NullPointerException

Open owonie opened this issue 1 year ago • 2 comments

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

  1. import Braze from "@braze/react-native-sdk".

  2. 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: Screenshot 2024-03-26 at 5 58 37 PM

@braze/react-native-sdk/src/braze.js Screenshot 2024-03-26 at 6 08 31 PM

@braze/react-native-sdk/android/main/java/com/braze/reactbridge/BrazeReactBridgeImpl.kt Screenshot 2024-03-27 at 9 53 37 AM

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: Screenshot 2024-03-27 at 10 06 24 AM 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 :)

owonie avatar Mar 27 '24 01:03 owonie

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?

jerielng avatar Mar 27 '24 14:03 jerielng

Thanks for your quickly reply. It's sufficient for my case, I will wait for your update. :)

owonie avatar Mar 27 '24 14:03 owonie

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!

vanessaland avatar May 02 '24 15:05 vanessaland