首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新修复数据库时指针异常

更新修复数据库时指针异常
EN

Stack Overflow用户
提问于 2021-12-27 01:39:52
回答 1查看 603关注 0票数 2

最近我偶然发现了一个运行时错误,它以任何原因使我的应用程序崩溃,我无法找到它的根源。基本上,我是通过将用户配置文件数据存储在hashmap中来更新它。这就是用户类的样子:

代码语言:javascript
复制
@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的活动中,我将移动和性别存储到一个hashmap中,然后调用Firebase函数来更新Firestore数据库。这是一种活动的方法。单击按钮"submit“时,将运行以下代码:

代码语言:javascript
复制
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
                )
            }
        }

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

代码语言:javascript
复制
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()
                    }
                }
            }


}

但是我发现了一个错误:

代码语言:javascript
复制
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)

我完全不知道空指针异常在哪里发生.有趣的是(这很重要),数据库确实得到了正确的更新,那么它为什么会崩溃呢?任何帮助都将不胜感激。:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-27 06:12:19

这个特定的错误最近出现了很多,这似乎是由于Firebase侦听器中的一个不一致的空注释。

显然谷歌知道问题所在可能是最近修好的

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

代码语言:javascript
复制
.addOnSuccessListener { 
    println("Updated doc")
}

代码语言:javascript
复制
.addOnSuccessListener { _: Void? ->
    println("Updated doc")
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70491011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档