简体   繁体   中英

Error: java.lang.IllegalArgumentException: Parameter specified as non-null is null firebase transaction kotlin

I am trying to increment an integer in Firebase database but, the app crashes.

abstract class BrowserActivity : ThemableBrowserActivity(),OnClickListener {

        private var mAuth: FirebaseAuth? = null 

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            BrowserApp.appComponent.inject(this)
            setContentView(R.layout.activity_main)

            incrementId() //calling the function increment id
        }

        protected fun incrementId() {
                 val cUser = mAuth?.currentUser
                 val uid: String = cUser!!.uid    
                 var databaseReference = FirebaseDatabase.getInstance().getReference("users").child(uid).child("id")


                 databaseReference!!.runTransaction(object : Transaction.Handler {
                        override fun doTransaction(mutableData: MutableData): Transaction.Result {
                            var count = mutableData.getValue(Int::class.java)
                            if (count == null) {
                                mutableData.setValue(1)
                            } else {
                                mutableData.setValue(count+1)
                            }
                            return Transaction.success(mutableData)
                        }

                        override fun onComplete(databaseError: DatabaseError, b: Boolean, dataSnapshot: DataSnapshot) {}
                    })
                }
        }

Logcat:

10-01 22:30:34.227 14674-14674/in.brew.meteor E/FileUtils: Unable to write bundle to storage 10-01 22:30:34.228 14674-14674/in.brew.meteor E/AndroidRuntime: FATAL EXCEPTION: main Process: in.brew.meteor, PID: 14674 java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter databaseError at in.brew.meteor.browser.activity.BrowserActivity$saveData$1.onComplete(BrowserActivity.kt) at com.google.android.gms.internal.zzecm.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Database structure:

my-firebase-app-name-9c826
   |__-users
         |_-FZTuHfNiXoSWOLiNlSv9w1vVZj03  //uid: user id of current user
                 |__id: 1 

Database screenshot

My Firebase rules: `My Firebase rules:

{ "rules": { ".read": true, ".write": true } }

With reference to this post, I am passing databaseError as null to onComplete . By default all variables in kotlin are non null. So, if I want to pass a null parameter, I have to add a ? to the parameter, which in my case should be DatabaseError? . So the onComplete function should look like:

override fun onComplete(databaseError: DatabaseError?, b: Boolean, dataSnapshot: DataSnapshot) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM