Search code examples
androidfirebasekotlinfirebase-realtime-databasekotlin-null-safety

Kotlin Firebase Realtime database transaction errors


I have the following code

   database.child("users").child(this.invitedBy).runTransaction(object : Transaction.Handler {
                override fun doTransaction(mutableData: MutableData): Transaction.Result {
                    val u = mutableData.getValue(User::class.java)
                        ?: return Transaction.success(mutableData)

                    if (u.numReferrals == null || u.referralEarning == null) {
                        // Unstar the post and remove self from stars
                        u.numReferrals = 1
                        u.referralEarning = 1;
                    } else {
                        // Star the post and add self to stars
                        u.numReferrals = u.numReferrals!! + 1
                        u.referralEarning = u.referralEarning!! + 1;
                    }

                    // Set value and report transaction success
                    mutableData.value = u
                    return Transaction.success(mutableData)
                }

                override fun onComplete(
                    databaseError: DatabaseError?,
                    committed: Boolean,
                    currentData: DataSnapshot?
                ) {
                    // Transaction completed
                    Log.d("MUser", "postTransaction:onComplete:" + databaseError!!)
                }
            })

When I run it, it gives me the following error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.test, PID: 5375
    kotlin.KotlinNullPointerException
        at com.test.test.MUser$setReferral$1.onComplete(MUser.kt:229)
        at com.google.firebase.database.core.Repo$16$1.run(com.google.firebase:firebase-database@@19.2.1:1006)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
W/System.err: kotlin.KotlinNullPointerException
W/System.err:     at com.test.test.MUser$setReferral$1.onComplete(MUser.kt:229)
        at com.google.firebase.database.core.Repo$16$1.run(com.google.firebase:firebase-database@@19.2.1:1006)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

Here's my User.kt

import com.google.firebase.database.IgnoreExtraProperties

// [START rtdb_user_class]
@IgnoreExtraProperties
data class User(
    var id: String = "",
    var name: String = "",
    var email: String = "",
    var userReads: Int? = 0,
    var userPlays: Int? = 0,
    var userWatches: Int? = 0,
    var paidDate : Int? = 0,
    var endPaidDate : Int? = 0,
    var paidStatus : String? = "",
    var paymentMethod : String? = "",
    var planid : String? = "",
    var firebaseUid : String? = "",
    var referralEarning : Int? = 0,
    var numReferrals : Int? = 0
)

What am I doing wrong?


Solution

  • Please take a look at onComplete method:

    override fun onComplete(
                        databaseError: DatabaseError?,
                        committed: Boolean,
                        currentData: DataSnapshot?
                    ) {
                        // Transaction completed
                        Log.d("MUser", "postTransaction:onComplete:" + databaseError!!)
                    }
    

    You are using !! operator when referencing databaseError parameter, but it can be null, so you can't use !!. To avoid that Exception just remove !! operator:

    Log.d("MUser", "postTransaction:onComplete:" + databaseError)
    

    databaseError: DatabaseError? - it is a nullable object (nullable objects marked with ? operator), you can't use !! operator on it if you are not 100% sure that it is not null.

    Also you can use ? operator on nullable objects to call its methods or properties, e.g.:

    Log.d("MUser", "postTransaction:onComplete:" + databaseError?.getMessage())
    

    More info about Null Safety in Kotlin.