在更新Firestore数据库时,Android Studio Kotlin出现空指针异常

2 人关注

我最近偶然发现了一个运行时错误,它使我的应用程序因某种原因而崩溃,但我无法找到它的根源。基本上,我正在更新用户配置文件数据,将其存储在一个哈希姆中。这就是用户类的模样。

@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)

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

3 个评论
你应该先用调试器来追踪代码,看看它到底在哪里崩溃。 另外,为了在Stack Overflow上发帖,你应该尽可能多地进行硬编码,不要让人们想当然地认为你的所有变量和方法都做了你认为的事情。
另外,无论是否相关,你都不应该把一个活动实例传递到你的数据层中,因为你将会泄露这个对象,并可能导致其他问题。
android
firebase
kotlin
google-cloud-firestore
nullpointerexception
Pestilence01
Pestilence01
发布于 2021-12-27
1 个回答
Tyler V
Tyler V
发布于 2021-12-27
已采纳
0 人赞同

这个特定的错误最近经常出现,似乎是由于Firebase监听器中不一致的nullability注释造成的。

显然,谷歌已经意识到 the issue 最近可能已经修复了它

在Kotlin中为 update 的调用添加 onSuccessListener 时,它会为 it 推断出一个非空类型( Void 不是 Void? )。这意味着如果Firestore返回一个空的结果(对于java中的 Void 参数来说,这并不罕见),当它被投到一个非空的参数时,它将引发你显示的错误。解决办法是更新到修复这个问题的最新版本,或者如果你需要立即解决这个问题,专门将类型设置为nullable,这样就可以改变

.addOnSuccessListener { 
    println("Updated doc")