身份验证和Firestore错误-NullPointerException

时间:2019-11-03 14:33:26

标签: android kotlin nullpointerexception google-cloud-firestore firebase-authentication

当我添加Firestore云依赖项时,用户注销时我的应用程序崩溃

   kotlin.KotlinNullPointerException
            at com.example.proghelp.ProfileActivity$read_write_fire$2.onEvent(ProfileActivity.kt:93)
            at com.example.proghelp.ProfileActivity$read_write_fire$2.onEvent(ProfileActivity.kt:35)
            at com.google.firebase.firestore.DocumentReference.zza(Unknown Source)
            at com.google.firebase.firestore.zzd.onEvent(Unknown Source)
            at com.google.android.gms.internal.firebase-firestore.zzkp.zza(Unknown Source)
            at com.google.android.gms.internal.firebase-firestore.zzkq.run(Unknown Source)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4517)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
            at dalvik.system.NativeStart.main(Native Method)

我的Firebase依赖项

 implementation 'com.google.firebase:firebase-core:15.0.0'
    implementation 'com.google.firebase:firebase-storage:15.0.0'
    implementation 'com.google.firebase:firebase-database:15.0.0'
    implementation 'com.google.firebase:firebase-auth:15.0.0'
    implementation 'com.google.android.gms:play-services-auth:15.0.0'
    implementation 'com.firebaseui:firebase-ui-database:3.1.3'
    implementation 'com.firebaseui:firebase-ui-storage:3.2.1'
    implementation 'com.google.firebase:firebase-analytics:15.0.0'
    implementation 'com.google.firebase:firebase-firestore:15.0.0'
    implementation 'com.android.support:multidex:1.0.3'

我的日志以该代码显示:

 var db = FirebaseFirestore.getInstance();
       val notebookRef3 = db.collection(currentuser.uid)

 notebookRef3.document("name").addSnapshotListener { querySnapshot, e ->
            var note = querySnapshot!!.toObject(Note2::class.java)
           var name = note!!.username
            iduserprofile.text = name
        }

它说日志在这里

   var note = querySnapshot!!.toObject(Note2::class.java)

1 个答案:

答案 0 :(得分:0)

querySnapshot可以为null,但是您正在使用!!运算符强制取消引用它,而无需先对其进行检查。不要这样做-始终检查是否为null。如果querySnapshot为null,则意味着异常e将为非null,您应该记录该异常。

这可能是一个权限错误,由于用户不再使用有效凭据登录,因此由于安全规则,导致侦听器失败。

相关问题