'how to get android on click listener to work despite nullpointerexception

My project throws a nullpointerexception buy attempting invoke an onclicklistener on a null object. here is my code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    buttonRegister = (Button) findViewById(R.id.buttonRegister);
    editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    editTextPassword =  (EditText) findViewById(R.id.editTextPassword);
    buttonSignin = (Button) findViewById(R.id.buttonSignin);
 buttonRegister.setOnClickListener(this);
buttonSignin.setOnClickListener(this);



    progressDialog = new ProgressDialog(this);
    firebaseAuth = FirebaseAuth.getInstance();
    if (firebaseAuth.getCurrentUser() != null){
        finish();
        startActivity(new Intent(getApplicationContext(), ProfileActivity.class));  //it is here where the error is
    }
  }
 public void onClick(View view) {
    if(view == buttonRegister){
        registerUser();
    }
    if (view == buttonSignin)
        startActivity(new Intent(this, LoginActivity.class));
}

and here is logcat

 at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.myapplication7.MainActivityOne.onCreate(MainActivityOne.java:53)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

and this is interesting because it worked perfectly for me yesterday but now it does not. what is wrong with the code?



Solution 1:[1]

You have to properly set the 'context' for the setOnClickListeners of your Buttons. Try doing this way:

 buttonRegister.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
               registerUser();
            }
        });

 buttonSignin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startActivity(new Intent(this, LoginActivity.class));
            }
        });

Also, you can use a more precise way to get the firebase authentication instance. As you are using email and password to login you can use the Firebase authentication's signInWithEmailAndPassword.

From this, you can also access the user credentials which might be helpful in other instances of your app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel