Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Adding Users Using Parse.com Error Messages with Dialogs

Null Pointer Exception crashing Ribbit.

I'm getting a null pointer exception when trying to assign mUsername.getText().toString() to username.

I know what a null pointer exception is, so please spare me the description, but my question is how can I avoid this null pointer exception? The point of this part is to see if all the fields were entered.

Any suggestions? Below is my code.

public class SignUpActivity extends ActionBarActivity {

protected EditText mUsername;
protected EditText mPassword;
protected EditText mEmail;
protected Button mSignUpButton;


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

    mUsername = (EditText)findViewById(R.id.usernameField);
    mPassword = (EditText)findViewById(R.id.passwordField);
    mEmail = (EditText)findViewById(R.id.emailField);
    mSignUpButton = (Button)findViewById(R.id.signupButton);

    mSignUpButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();
            String email = mEmail.getText().toString();

            username = username.trim();
            password = password.trim();
            email = email.trim();

            if (username.isEmpty() || password.isEmpty() || email.isEmpty()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
                builder.setMessage(R.string.signup_error_message)
                    .setTitle(R.string.signup_error_title)
                    .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                //create new user
            }

        }
    });
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

4 Answers

Ahh. I figured it out. It was ambiguous naming in my XML. Thanks anyways.

Randall Keels
Randall Keels
1,650 Points

Hi Marco,

I am having a NullPointerException in my Ribbit App also but I am earlier in the app development. I am trying to get the OnClickListener to work with the signupText. I followed Ben's lesson to a T but I don't see why it is giving a null error.

Thanks in advance for your support.

Hi Randall,

Post the relevant code here and I'll see if I can help you figure it out.

Marco

Randall Keels
Randall Keels
1,650 Points

Hi have already started another thread here: LoginActivity.java ~ OnClickListener > signupText EXPLOSION