-1

when I added Firestore cloud dependency, my app crashes when the user logs out

   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)

my firebase dependencies

 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'

my log is in tis code:

 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
        }

it says the log is here

   var note = querySnapshot!!.toObject(Note2::class.java)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

querySnapshot can be null, but you're forcefully dereferencing it with the !! operator without checking it first. Don't do that - always check for null. If querySnapshot is null, that means the exception e will be non-null, and you should be logging that instead.

It's probably a permission error that causes the listener to fail due to security rules since the user is no longer signed in with valid credentials.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441