0

EDIT: I don't know why this was marked as a duplicate. I know nullpointerexception questions must be common, but in this case the object was initialised and defined properly. The question you linked to doesn't solve my problem at all.

I'm getting an error and I can't understand why. The class was working a few days ago and I'm sure I haven't changed anything. It's a null pointer exception for a button, but I've done it the same way for all my buttons and the rest work. What am I missing here?

This is the error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Here is the code:

public class Login extends AppCompatActivity implements View.OnClickListener {

Button bLogin;
EditText etUsername, etPassword;

UserLocalStore userLocalStore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);

    bLogin = (Button) findViewById(R.id.bLogin);
    bLogin.setOnClickListener(this);     //This is the line the exception points to
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.bLogin:
            String username = etUsername.getText().toString();
            String password = etPassword.getText().toString();

            User user = new User(username, password);

            authenticate(user);
            break;
    }
}

private void authenticate(User user){
    ServerRequests serverRequests = new ServerRequests(this);
    serverRequests.fetchUserDataInBackground(user, new GetUserCallback() {
        @Override
        public void done(User returnedUser) {
            if(returnedUser == null){
                showErrorMessage();
            }else{
                logUserIn(returnedUser);
            }
        }
    });
}

private void showErrorMessage(){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Login.this);
    dialogBuilder.setMessage("Incorrect user details");
    dialogBuilder.setPositiveButton("Ok", null);
    dialogBuilder.show();
}

private void logUserIn(User returnedUser){
    userLocalStore.storeUserData(returnedUser);
    userLocalStore.setUserLoggedIn(true);
}
}

And the XML, just in case:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp">

<EditText
    android:id="@+id/etUsername"
    android:hint="Username"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginTop="50dp"
    android:layout_marginBottom="10dp" />

<EditText
    android:id="@+id/etPassword"
    android:hint="Password"
    android:inputType="textPassword"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp" />

<Button
    android:id="@+id/bLogin"
    android:text="Login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

Any help is appreciated, thanks.

Jack Finan
  • 99
  • 1
  • 8
  • try rebuilding / redeploying the project –  Mar 20 '16 at 17:31
  • your code runs fine if I run it locally without making any changes! try cleaning the project and running once! – sujay Mar 20 '16 at 17:38
  • I feeling like you are not loading the proper view! thats one possible situation when findViewById returns null checkout this answer http://stackoverflow.com/a/7876664/3000299 – sujay Mar 20 '16 at 17:43
  • I tried cleaning and rebuilding but it still crashes. This is so weird.. – Jack Finan Mar 20 '16 at 17:44
  • Solved it, thanks Jay. The XML I was using was named content_login.xml and was included in activity_login.xml. Turns out my project partner changed activity_login.xml to be the full layout and named the button btnLogin instead of bLogin. Such a stupid thing to cause so much frustration! – Jack Finan Mar 20 '16 at 17:56

0 Answers0