1
votes

Issue Fixed Update: Fixed by downgrading Firebase to 9.6.0 (just a random version I chose). I'm assuming the original issue has something to do with the emulator's good play or something.

Old Issue: So I need help, trying to set up Firebase Auth for logging in a user. I've enable this in my Firebase console and I've done this before and had it working as a class assignment weeks ago. But this time I'm getting a Null Object Reference when trying to login.

I'm starting to think this is related to my gradle files. mAuth isn't null, String email and password aren't null. I'm simply following this Firebase guide: https://firebase.google.com/docs/auth/android/password-auth

Exact Logcat error here

Module app Gradle file here

Project Gradle file here

Full class below. Line 43 where the error begins (as from the screenshot) is 'mAuth.signInWithEmailAndPassword(email, password)'

package com.example.bri.inclass09;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class LoginActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private EditText emailET, passwordET;
private Button login, signup;
private static final String TAG = "test";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    mAuth = FirebaseAuth.getInstance();
    Log.d(TAG, "mAuth is: " + mAuth.toString());
    login = findViewById(R.id.loginButton);
    signup = findViewById(R.id.signupButton);
    emailET =  findViewById(R.id.loginEmailET);
    passwordET = findViewById(R.id.loginPassET);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = emailET.getText().toString();
            String password = passwordET.getText().toString();
            Log.d(TAG, email + " " + password);

            mAuth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign in success, update UI with the signed-in user's information
                                Log.d(TAG, "createUserWithEmail:success");
                                FirebaseUser user = mAuth.getCurrentUser();
                            } else {
                                // If sign in fails, display a message to the user.
                                Log.w(TAG, "createUserWithEmail:failure", task.getException());
                                Toast.makeText(LoginActivity.this, "Authentication failed.",
                                        Toast.LENGTH_SHORT).show();
                            }

                            // ...
                        }
                    });
        }
    });
}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    //FirebaseUser currentUser = mAuth.getCurrentUser();
}

}

2

2 Answers

0
votes

In your app module build.gradle file:

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

is misplaced. It should be the very last line, outside the dependencies block.

In addition, the device or emulator must have a version of Google Play services installed that is compatible with the version of Firebase you build with. If you are seeing this problem on a device, you can go to the Play Store to download the latest version. If you are using an emulator, you need to use the SDK Manager to download the latest Google APIs System Image for the emulator you are using.

0
votes

in Android studio verify everything is correct using tools, firebase, Authentication then follow the prompts and verify your current key either debug or release (whichever your using) matches the setup if correct after the prompts you should get green "OK" then make sure your key matches the one in firebase

Tools

enter image description here