Skip to content
Advertisement

Can’t get custom Object from Firestore

I’m trying to create a custom object from a document in the Firebase Firestore. Whenever I try to use the User object, I get a NullPointerException on the user object:

JavaScript

I call getUserFromFirebaseUser here:

JavaScript
JavaScript

Here is the User class:

JavaScript

I’m relatively new to using Firebase with Android. Any help is appreciated.

EDIT I’ve added Log statements to the onComplete() method and surrounding the getUserFromFirebaseUser() call in updateFirestoreDatabase().

JavaScript

So, I think the onCompleteListener is not able to finish

Advertisement

Answer

You are trying to use user.getShortEmail() while user is null. That’s because user hasn’t yet been assigned a value. This is becasue docRef.get().addOnCompleteListener() is asynchronous and returns immediately, before the query is complete. If you put logs around and inside the callback, it will become more clear in what order things are actually happening.

You’re probably going to have to take up some form of asynchronous programming. In modern android, that’s going to be Kotlin coroutines or LiveData, and your code will look very little like it does now.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement