Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception: "null cannot be cast to non-null type java.util.HashMap" while sending event with null arguments #86

Open
mahdinba97 opened this issue May 12, 2024 · 1 comment

Comments

@mahdinba97
Copy link

Hi. thank you for the great package.
I have a custom event function which calls the recordCustomEvent function of NewRelicMobile, and I get this exception when the eventAttributes map is null.

E/MethodChannel#newrelic_mobile( 5164): java.lang.NullPointerException: null cannot be cast to non-null type java.util.HashMap<*, *>{ kotlin.collections.TypeAliasesKt.HashMap<*, *> }
E/MethodChannel#newrelic_mobile( 5164): 	at com.newrelic.newrelic_mobile.NewrelicMobilePlugin.onMethodCall(NewrelicMobilePlugin.kt:141)
E/MethodChannel#newrelic_mobile( 5164): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/MethodChannel#newrelic_mobile( 5164): 	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/MethodChannel#newrelic_mobile( 5164): 	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/MethodChannel#newrelic_mobile( 5164): 	at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#newrelic_mobile( 5164): 	at android.os.Handler.handleCallback(Handler.java:942)
E/MethodChannel#newrelic_mobile( 5164): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#newrelic_mobile( 5164): 	at android.os.Looper.loopOnce(Looper.java:201)
E/MethodChannel#newrelic_mobile( 5164): 	at android.os.Looper.loop(Looper.java:288)
E/MethodChannel#newrelic_mobile( 5164): 	at android.app.ActivityThread.main(ActivityThread.java:7918)
E/MethodChannel#newrelic_mobile( 5164): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#newrelic_mobile( 5164): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/MethodChannel#newrelic_mobile( 5164): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

I believe this is the line of the plugin native code which causes this exception:

// NewrelicMobilePlugin.kt
"recordCustomEvent" -> {
                val eventType: String? = call.argument("eventType")
                val eventName: String? = call.argument("eventName")
                val eventAttributes: HashMap<String, Any>? = call.argument("eventAttributes")

                val copyOfEventAttributes = eventAttributes?.clone() as HashMap<*, *>; //  <--  causing exception
                for (key in  copyOfEventAttributes.keys)  {
                    val value = copyOfEventAttributes[key]
                    if(value is HashMap<*, *>) {
                        for (k in value.keys) {
                            value[k]?.let { eventAttributes.put(k as String, it) };
                        }
                        eventAttributes.remove(key)
                    }
                }
                val eventRecorded =
                    NewRelic.recordCustomEvent(eventType, eventName, eventAttributes)
                result.success(eventRecorded)
            }

here is my code:

static final _newRelic = NewrelicMobile.instance;

//...

static Future<bool> sendEvent({
  required NewRelicEventType eventType,
  String eventName = '',
  Map<String, dynamic>? eventAttributes,
}) {
  return _newRelic.recordCustomEvent(
    eventType.toString(),
    eventName: eventName,
    eventAttributes: eventAttributes,
  );
}

My environment is:
Flutter 3.16.9
NewRelicMobile 1.0.2
agent-gradle-plugin 6.5.0

@ndesai-newrelic
Copy link
Collaborator

@mahdinba97 we will fix it for next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants