Home > Software design >  Firebase addOnCuccessListener{} causes nullPointerException
Firebase addOnCuccessListener{} causes nullPointerException

Time:12-23

I am updating a value in the realtime database with the following code :-

  val databaseRef = FirebaseDatabase.getInstance().getReference("Services")
        val service = mapOf<String, String>(
            "cost" to serviceCost,
            "name" to serviceName
        )


        databaseRef.child(name!!).updateChildren(service)
            .addOnSuccessListener {
                Toast.makeText(this, "Service Updated", Toast.LENGTH_LONG).show()
                startActivity(Intent(this, MainActivity::class.java))
            }.addOnFailureListener {
                Toast.makeText(this, "Connection Problem", Toast.LENGTH_LONG).show()
            }

The database is being updated exactly as I need but the application crashes with the following error message :-

msg: java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter it

stacktrace: java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter it
at com.example.prototypeadmin.UpdateServiceActivity.onCreate$lambda-2$lambda-0(Unknown Source:7)
at com.example.prototypeadmin.UpdateServiceActivity.$r8$lambda$OCfvRKDg4t88LbX2Kozztfb3Rnw(Unknown Source:0)
at com.example.prototypeadmin.UpdateServiceActivity$$ExternalSyntheticLambda2.onSuccess(Unknown Source:4)
at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.0:1)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:594)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Without the .addOnSuccessLister{} the app does not crash but I need to perform the Intent and Toast only on success. I am new to Firebase realtime database. Please explain what should I do.

Thanks in advance ! , ask for any extra information if required.

CodePudding user response:

I updated firebase-analytics in my app build.gradle file and I stopped getting this error.

implementation 'com.google.firebase:firebase-analytics-ktx:20.0.2'
  • Related