title: NullPointerException FirebaseAuth signInWithEmailAndPassword
body: <p>I moved my Firebase <code>signInWithEmailAndPassword()</code> method out of my log-in activity to a separate network package/class. I had done this with the <code>createUserWithEmailAndPassword()</code> method and it is working as expected. However, the <code>signInWithEmailAndPassword()</code> method throws a</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method
    'com.google.android.gms.tasks.Task
    com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(java.lang.String,
    java.lang.String)' on a null object reference</p>
  </blockquote>
  
  <p>Thrown at line 116 in my <code>FirebaseBacked.java</code> network class that was initiated by a <code>LoginActivity.java</code> <code>onClick()</code> at line 102.</p>
  
  <p>I'm not sure how to continue from here. Any help with code example will be greatly appreciated.</p>
  
  <p><strong>LoginActivity:</strong></p>
  
  <pre><code>public class LoginActivity extends FirebaseBackend {
  
  // UI references.
  private ShowPassword inputPassword;
  private AutoCompleteTextView inputEmail;
  private FirebaseAuth auth;
  private ProgressBar progressBar;
  private Context context;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.context = this;
  
      // Get Firebase auth instance
      auth = FirebaseAuth.getInstance();
  
      if (auth.getCurrentUser() != null) {
          startActivity(new Intent(LoginActivity.this, MainActivity.class));
          finish();
      }
  
      // Inflate interfaces
      setContentView(R.layout.activity_login);
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      inputEmail = (AutoCompleteTextView) findViewById(R.id.email);
      inputPassword = (ShowPassword) findViewById(R.id.password);
      progressBar = (ProgressBar) findViewById(R.id.progressBar);
      Button btnSignup = (Button) findViewById(R.id.btn_signup);
      Button btnLogin = (Button) findViewById(R.id.btn_login);
      Button btnReset = (Button) findViewById(R.id.btn_reset_password);
  
      btnSignup.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
          }
      });
  
      btnReset.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
          }
      });
  
      btnLogin.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              String email = inputEmail.getText().toString();
              final String password = inputPassword.getText().toString();
              View focusView;
  
              if (inputEmail.getText().length() == 0 || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
                  inputEmail.setError(getString(R.string.error_invalid_email));
                  focusView = inputEmail;
                  focusView.requestFocus();
                  return;
              }
  
              if (!PasswordValidateUtility.isValidPassword(inputPassword.getText().toString().trim())) {
                  inputPassword.setError(getString(R.string.error_invalid_password));
                  focusView = inputPassword;
                  focusView.requestFocus();
                  return;
              }
  
              progressBar.setVisibility(View.VISIBLE);
              signInWithMyApp();
          }
      });
  }
  
  }
  </code></pre>
  
  <p><strong>FirebaseBackend</strong></p>
  
  <pre><code>public class FirebaseBackend extends AppCompatActivity {
  
      protected AutoCompleteTextView inputEmail;
      protected ShowPassword inputPassword;
      protected ProgressBar progressBar;
      protected FirebaseAuth auth;
      protected Context context;
  
      public void createNewUser() {
  
          String email = inputEmail.getText().toString().trim();
          String password = inputPassword.getText().toString().trim();
  
          progressBar.setVisibility(View.VISIBLE);
  
          //create user
          auth.createUserWithEmailAndPassword(email, password)
                  .addOnCompleteListener(FirebaseBackend.this, new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          progressBar.setVisibility(View.GONE);
                          /**
                           * If creating the user in the FireBase backend fails, display a message
                           * to the user with a dialog. If sign in succeeds, the auth state listener
                           * will be notified and the logic to handle the signed in user can be
                           * handled by the listener.
                           */
                          if (!task.isSuccessful()) {
                              /**
                               * FireBase failed creating the user on the backend. Most likely because
                               * the email address is already in use or there was a connection issue.
                               * We will show the user a `Failed Dialog` and ask them to try again.
                               */
                              // Signup Failed Dialog
                              DialogChooser.failCreateUser(context);
                          } else {
                              /**
                               * The user was successfully created on the backend, so now ask FireBase
                               * to send an authentication email to the new user.
                               */
                              sendVerificationEmail();
                          }
  
                      }
                  });
      }
  
      public void sendVerificationEmail() {
  
          FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  
          user.sendEmailVerification()
                  .addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;Void&gt; task) {
                          if (!task.isSuccessful()) {
                              /**
                               * FireBase failed to send a verification email so we will show a
                               * `Failed Send Verification Dialog` to the user, sign out the user
                               * and override the pent.
                               */
                              overridePendingTransition(0, 0);
                              FirebaseAuth.getInstance().signOut();
                              startActivity(getIntent());
                              DialogChooser.failSendVerification(context);
                          } else {
                              /**
                               * FireBase successfully sent a verification email so now we show a
                               * `Thank-you Dialog` to the user that allows them to check their email
                               * for the verification.
                               */
                              DialogChooser.agreementAccepted(context);
                              FirebaseAuth.getInstance().signOut();
                          }
                      }
                  });
      }
  
      public void signInWithMyApp() {
  
          String email = inputEmail.getText().toString();
          final String password = inputPassword.getText().toString();
  
          //Get Firebase auth instance - Just added
          auth = FirebaseAuth.getInstance();
  
          auth.signInWithEmailAndPassword(email, password)
                  .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          /**
                           * If sign in fails, display a message to the user. If sign in succeeds
                           * the auth state listener will be notified and logic to handle the
                           * signed in user can be handled in the listener
                           */
                          progressBar.setVisibility(View.GONE);
  
                          if (!task.isSuccessful()) {
                              /**
                               * If the user attempts to sign in with an account that is
                               * already in use, we'll show then a `FailSignIn Dialog` letting
                               * them know and give them the option to try with another email
                               * ID or signup with a new email address.
                               */
                              DialogChooser.failSignIn(context);
                          } else {
                              // Check if the user has been verified.
                              checkIfEmailVerified();
                          }
                      }
  
                  });
  
      }
  
      public void checkIfEmailVerified() {
  
          FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  
          if (user.isEmailVerified()) {
              /**
               * If the user is verified, finish this activity and send them to the
               * MainActivity and show a success toast message.
               */
              Toast.makeText(this, getString(R.string.toast_login_success), Toast.LENGTH_SHORT).show();
              Intent intent = new Intent(this, MainActivity.class);
              this.startActivity(intent);
              finish();
          } else {
              /**
               * If the email is not verified, prompt a message to the user using
               * the `failEmailVerified` dialog and make sure the user is still
               * signed out.
               */
              progressBar.setVisibility(View.GONE);
              DialogChooser.failEmailVerified(context);
              FirebaseAuth.getInstance().signOut();
          }
      }
  }
  </code></pre>