Kaiser
Kaiser

Reputation: 606

How to resolve Nullpointer exception after user logs in for the second time in Firebase app?

When an email verified user logs in for the first time in my app everything works as expected (he gets to a welcome screen and then to the MainActivity) but when he logs out out and logs in again there is a null pointer exception. When a user logs in for the second time he should go straight to the MainActivity and not to the welcome screen which you see in the below code.

This was a known issue about a year ago so my question is how to resolve this issue? Has this bug been fixed and when yes what did I do wrong in my code?

The question I am referring is here :Firebase user returns null metadata for already signed up users

Here is the code I am using to check whether the user logs in for the first or second time:

...

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        auth = FirebaseAuth.getInstance();
        FirebaseUser firebaseUser = auth.getCurrentUser();

        if (firebaseUser != null && firebaseUser.isEmailVerified()) {
            startActivity(new Intent(LoginEmailActivity.this, MainActivity.class));
            finish();
        }

        setContentView(R.layout.activity_email_login);

        ...

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ...

                progressBar.setVisibility(View.VISIBLE);
                final FirebaseUserMetadata metadata = auth.getCurrentUser().getMetadata();

                //authenticate user
                auth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(LoginEmailActivity.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {

                                progressBar.setVisibility(View.GONE);
                                if (!task.isSuccessful()) {
                                        Toast.makeText(LoginEmailActivity.this, "Ups, es ist wohl etwas schief gelaufen. Bitte überprüfe deine Internetverbindung und versuche es erneut.", Toast.LENGTH_LONG).show();

                                } else {

                                    if (auth.getCurrentUser().isEmailVerified() && !(metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp())){
                                        Intent intent = new Intent(LoginEmailActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();

                                    } else if (auth.getCurrentUser().isEmailVerified() && (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp())){

                                        firstLoginScreen();
                                        finish();
                                    }
                                    else if (!auth.getCurrentUser().isEmailVerified()){
                                        Toast.makeText(LoginEmailActivity.this, "Bitte verifiziere erst deine E-Mail Adresse mit dem Link, den wir dir geschickt haben.", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        });
            }
        });

...

Here is the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUserMetadata com.google.firebase.auth.FirebaseUser.getMetadata()' on a null object reference at com.example.android.guessit.LoginRegistration.LoginEmailActivity$1.onClick

Also the "getCreationTimestamp" says that it may produce a NullPointerException

Upvotes: 0

Views: 322

Answers (1)

Roberto Martucci
Roberto Martucci

Reputation: 1267

The problem is where you call

final FirebaseUserMetadata metadata = auth.getCurrentUser().getMetadata()  

Calling it before completing the user login, brings you to the NullPointer you are facing. Just move it inside the onComplete callback, where the current user has a value.

Also, just for code optimisation add a return statement when you switch to the MainActivity.

if (firebaseUser != null && firebaseUser.isEmailVerified()) {
     startActivity(new Intent(LoginEmailActivity.this, MainActivity.class));
     finish();
     return;
}

You need a return in your if statement when you go to the MainActivity. Otherwise after that you set again setContentView(R.layout.activity_email_login).

Upvotes: 1

Related Questions