-1

Trying to create an app login page. however, it keeps giving me an error

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference

the problematic code is here

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_login);

    FirebaseAuth mAuth;

    mEmail = findViewById(R.id.input_email);
    mPassword = findViewById(R.id.input_password);
    String Email = mEmail.getText().toString().trim();
    String Password = mPassword.getText().toString().trim();
    if(fAuth.getCurrentUser() != null){
        startActivity(new Intent(getApplicationContext(),MainActivity.class));
        finish();
    }
  • 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) – a_local_nobody Dec 02 '20 at 09:24

1 Answers1

0

Try using this...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_login);

    FirebaseAuth fAuth = FirebaseAuth.getInstance();

    mEmail = findViewById(R.id.input_email);
    mPassword = findViewById(R.id.input_password);
    //Avoid these lines
    //String Email = mEmail.getText().toString().trim();
    //String Password = mPassword.getText().toString().trim();
    //This should be done only after user enter data preferably on button click
    if(fAuth.getCurrentUser() != null){
        startActivity(new Intent(getApplicationContext(),MainActivity.class));
        finish();
    }
Vishnu
  • 663
  • 3
  • 7
  • 24