1

I am trying to integrate google sign in provided by firebase auth in Android.I did everything same as given in firebase documentation.When user select account to sign in app stops and shows an error in log cat window given below.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual 
method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.signInWithCredential(com.google.firebase.auth.AuthCredential)' on a null object reference
    at com.app.bookbudi.Login.firebaseAuthWithGoogle(Login.java:99)
    at com.app.bookbudi.Login.onActivityResult(Login.java:86)

Here is my code:

Login.java

public class Login extends AppCompatActivity {

SignInButton signIn;
GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 1;

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

signIn = findViewById(R.id.signIn);

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).
            requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();

mGoogleSignInClient = GoogleSignIn.getClient(this,gso);

signIn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent signInIntent = mGoogleSignInClient.getSignInIntent();
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }
    });

 }

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RC_SIGN_IN){

        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);

        try{
        GoogleSignInAccount account = task.getResult(ApiException.class);
        firebaseAuthWithGoogle(account);

    }catch(ApiException e){

            Toast.makeText(getApplicationContext(),"Login failed",TastyToast.LENGTH_SHORT).show();
        }
    }
  }

 private void firebaseAuthWithGoogle(GoogleSignInAccount acct){

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(),null);

    fAuth.signInWithCredential(credential).addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {

        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if(task.isSuccessful()){

                Intent i = new Intent(Login.this,MainActivity.class);
                startActivity(i);
                finish();
            }
            else{

                Toast.makeText(getApplicationContext(),"Unable to login with google.",TastyToast.LENGTH_SHORT).show();

            }
        }
    });


  }

}

Please let me know what I did wrong in above code.

THANKS

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Digvijay
  • 2,887
  • 3
  • 36
  • 86

1 Answers1

2

You didn't initialize the Firebase Auth.

SignInButton signIn;
GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 1;
//Add Firebase Auth
private FirebaseAuth fAuth;

At this onCreate

fAuth = FirebaseAuth.getInstance(); 
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46