FirebaseAuth.getInstance() returns null

Viewed 194

I have a problem with getting mAuth.getInstance() from Firebase. It always returns null. Because of this problem, the whole app crashes and I cannot move on. How can I solve this problem?

I've tried to modify gradle versions, but those did not solve anything. I added the necessary code snippets here.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smartfarmandroidapp"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        tools:node="replace"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SmartFarmAndroidApp">
        <activity
            android:name=".MVVM.View.Activities.DrawerActivity"
            android:label="Drawer Activity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan"/>
        <activity android:name=".MVVM.View.Activities.MainActivity" />
        <activity android:name=".MVVM.View.Activities.LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Gradle :module

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

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.smartfarmandroidapp"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    //Firebase Authentication
    implementation platform('com.google.firebase:firebase-bom:28.0.1')
    implementation 'com.firebaseui:firebase-ui-auth:7.1.1'
    implementation 'com.google.firebase:firebase-auth'
}

Gradle :project

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }


    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.2'
        classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5'
        classpath 'com.google.gms:google-services:4.3.8'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

LoginActivity.java

 private static final int SIGN_IN = 42;
    private static final String TAG = "LoginActivity";
    private FirebaseAuth mAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FirebaseApp.initializeApp(this);
        mAuth = FirebaseAuth.getInstance(); //This line returns null 
    }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseUser currentUser = mAuth.getCurrentUser();
    }

The entire error exception which I get is the following:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.smartfarmandroidapp, PID: 16328
    java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
        at com.example.smartfarmandroidapp.MVVM.View.Activities.LoginActivity.onStart(LoginActivity.java:191)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
        at android.app.Activity.performStart(Activity.java:8024)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2 Answers

I've tried to modify Gradle versions, but those did not solve anything.

It's not about the Gradle version at all. Besides that, there is no need to call:

FirebaseApp.initializeApp(this);

If you integrate Firebase using the official documentation. If you do a standard integration, Firebase will be initialized automatically at the app launch.

The problem in your code lies in the fact that:

tools:node="replace"

It is present in your AndroidManifest file. That should be present only if want to merge manifest files.

To solve this, simply remove it and it will work perfectly fine.

  1. Did you import your json file from Firebase?

  2. Something is weird with your gradle file.

    • Move apply plugin: 'com.google.gms.google-services' before the dependencies block
    • Instead of apply plugin: 'com.android.application' use plugins { id 'com.android.application' } as first line of gradle.
Related