更新FiRestore数据库时Android Studio Kotlin空指针异常 [英] Android Studio Kotlin Null Pointer Exception while updating Firestore Database

查看:129
本文介绍了更新FiRestore数据库时Android Studio Kotlin空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近偶然发现了一个运行时错误,无论出于什么原因,它都会使我的应用程序崩溃,而我无法弄清它的真相。基本上,我通过将用户配置文件数据存储在哈希图中来更新用户配置文件数据。User类如下所示:

@Parcelize
data class User(
    val id: String = "",
    val firstName: String = "",
    val lastName: String = "",
    val email: String = "",
    val image: String = "",
    val mobile: Long = 0,
    val gender: String = "",
    val profileCompleted: Int = 0): Parcelable

在我名为UserProfileActivity的活动中,我将手机和性别存储到哈希图中,然后调用Firebase函数来更新Firestore数据库。以下是该活动的一种方法。单击"提交"按钮时,将运行以下代码:

btn_submit.setOnClickListener {
            if(validateUserProfileDetails()){  //checks if entered credentials are valid
                val userHashMap = HashMap<String, Any>()  //creates the hashmap

                val mobileNumber = et_mobile_number.text.toString().trim { it <= ' ' }

                val gender = if (rb_male.isChecked) {  //these are radio buttons, only 1 clicked
                    Constants.MALE
                } else {
                    Constants.FEMALE
                }

                userHashMap[Constants.MOBILE] = mobileNumber.toLong()  //storing info in hashmap

                userHashMap[Constants.GENDER] = gender

                showProgressDialog(resources.getString(R.string.please_wait))  //starting a progress dialog

                FirestoreClass().updateUserProfileData(  //method in FirestoreClass 
                    this, userHashMap
                )
            }
        }

现在与数据库通信的调用方法:

fun updateUserProfileData(activity: Activity, userHashMap: HashMap<String, Any>) {
        mFireStore.collection(Constants.USERS)  // collection named "users"
            .document(getCurrentUserID())  //getCurrentUserID() just gets the current user id 
            .update(userHashMap)  // hashmap used to update the user
            .addOnSuccessListener {
                when (activity) {
                    is UserProfileActivity -> {
                        activity.userProfileUpdateSuccess() //this just hides the progress dialog and finishes the activity in the UserProfileActivity
                    }
                }
            }
            .addOnFailureListener { e ->
                when (activity) {
                    is UserProfileActivity -> {
                        activity.hideProgressDialog()
                    }
                }
            }


}

但我收到此错误:

2021-12-27 02:35:38.727 14960-14960/com.example.myshopapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myshopapp, PID: 14960
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter it
    at com.example.myshopapp.firestore.FirestoreClass.updateUserProfileData$lambda-2(Unknown Source:7)
    at com.example.myshopapp.firestore.FirestoreClass.$r8$lambda$vs4EuaGwStcL-i3lXRUecduDHWM(Unknown Source:0)
    at com.example.myshopapp.firestore.FirestoreClass$$ExternalSyntheticLambda4.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:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
我完全不知道空指针异常发生在哪里.有趣的是(这一点很重要),数据库确实得到了正确的更新,那么为什么它会崩溃呢?任何帮助都将不胜感激。:)

推荐答案

此特定错误最近经常出现,似乎是由于Firebase侦听器中的可空性批注不一致所致。

显然Google知道the issuemay have recently fixed it

在Kotlin中为update调用添加onSuccessListener时,它会推断it的非空类型(VoidNOTVoid?)。这意味着如果Firestore返回NULL结果(这对于java中的Void参数来说并不少见),它将引发您在将其强制转换为非NULL参数时所显示的错误。解决方案是更新到修复此问题的最新版本,或者如果您需要立即解决问题以专门将类型设置为可空,则请更改

.addOnSuccessListener { 
    println("Updated doc")
}

.addOnSuccessListener { _: Void? ->
    println("Updated doc")
}

这篇关于更新FiRestore数据库时Android Studio Kotlin空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆