Phantomazi
Phantomazi

Reputation: 408

Application terminates when FirebaseAuth.getInstance() returns "null"

So I am starting with Firebase and I was going through this recently posted tutorial online.

At one point we have to start the application and there is this bit of code:

// Initialize Firebase Auth
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();

if (mFirebaseUser == null) {
    // Not logged in, launch the Log In activity
    loadLogInView();
}

This is placed in the onCreate(). The issue is that whenever I try to run the application, I get the following exception:

java.lang.IllegalArgumentException: Given String is empty or null` for line 28 which is `mFirebaseAuth = FirebaseAuth.getInstance();

Why am I getting null? And apart from that, what prevents the application from running despite that I am getting null? I am already treating the null in a way anyway.

The code never reaches the next line, it just stops at getting the Auth instance.

Upvotes: 3

Views: 1836

Answers (3)

Garvit Jain
Garvit Jain

Reputation: 2112

Go to project level build.gradle & check if it looks exactly like this:

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

and the code suddenly works and when you'll look at other answers you will find the same.

Upvotes: 1

Abhishek
Abhishek

Reputation: 1261

Please check your app Gradle file. And please make sure that the following line is added at the bottom

apply plugin: 'com.google.gms.google-services'

If this line is missing, then your code will compile fine, but FirebaseAuthwill fail to instantiate/initialize without reasonable warning.

Another possibility could be that your Authentication is not properly enabled in the Firebase Console. Please enable accordingly. Screenshot attached. enter image description here

All the best!

Upvotes: 0

Prakhar Sharma
Prakhar Sharma

Reputation: 1

I am not very sure about this, but i believe your mFireBaseAuth is not initialised yet. Maybe.

I guess this because I read in firebase docs : Note: getCurrentUser might also return null because the auth object has not finished initializing. If you use a listener to keep track of the user's sign-in status, you don't need to handle this case.

Upvotes: 0

Related Questions