0

How Do I solve this issue?

Java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.firebase.auth.FirebaseUser.isEmailVerified()' on a null object reference

Here is My Code

private void signIn(){
    loadingLogin(true);
    FirebaseUser firebaseUser = mAuth.getCurrentUser();
    mAuth.signInWithEmailAndPassword(binding.inputEmailL.getText().toString(), binding.inputPasswordL.getText().toString())
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if(task.isSuccessful() && firebaseUser.isEmailVerified() && task.getResult()!=null){
                        
                        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                        finish();

                    }else if (!firebaseUser.isEmailVerified()){
                        loadingLogin(false);
                        showToast("Please verify your email");
                    }else{
                        loadingLogin(false);
                        showToast("Login failed, please try again");
                    }
                }
            });


}
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – tgdavies Mar 12 '22 at 10:16

1 Answers1

0

Correct Answer

private void signIn(){

    loadingLogin(true);

    mAuth.signInWithEmailAndPassword(binding.inputEmailL.getText().toString(), binding.inputPasswordL.getText().toString())
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    FirebaseUser firebaseUser = mAuth.getCurrentUser();
                    if(task.isSuccessful() && firebaseUser!=null && firebaseUser.isEmailVerified() && task.getResult()!=null){

                        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                        finish();

                    }else if (firebaseUser!=null && !firebaseUser.isEmailVerified()){
                        loadingLogin(false);
                        showToast("Please verify your email");
                    }else{
                        loadingLogin(false);
                        showToast("Login failed, please try again");
                    }
                }
            });


}