2

Get KotlinNullPointerException when open initial fragment when init my uid:

fun setInitialData() {
        firebaseUser = FirebaseAuth.getInstance().currentUser
        databaseReference = FirebaseDatabase.getInstance().reference
        uid = firebaseUser!!.uid

But if I replace initialization on firebaseUser?.uid database data will not load immediately. So understand this the right approach? Also i understand how extra the decision to do some pause before data is loaded?

2
  • @JeelVankhede ok, but as i say, if i use firebaseUser?.uid, I have a problem with uploading data. Commented Oct 21, 2019 at 8:45
  • can you post the full error? Commented Oct 21, 2019 at 9:27

2 Answers 2

1

According to the official documentation regarding FirebaseUser's getCurrentUser() method:

Returns the currently signed-in FirebaseUser or null if there is none.

And to answer your question:

So understand this the right approach?

No, you need to always use getCurrentUser() != null to check if a user is signed in or not.

Also i understand how extra the decision to do some pause before data is loaded?

There is no need to pause something in this case.

Sign up to request clarification or add additional context in comments.

Comments

0

Firebase auth can't be initialized when an activity or a fragment is created as it takes time to become responsive. Try to pass it in a button click or anywhere other than oncreate.

1 Comment

i tried to init my firebase in onViewCreated. In method : setInitialData(). U can check full code here: gist.github.com/mnewlive/56c4a84eb3ef22f930779e49c710fc4a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.