1
newSubmitButton = (Button) findViewById(R.id.newPlayerSubmit);
Log.v("heeelp",""+newSubmitButton);

Seems simple enough. I have a global Button variable called newSubmitButton. I fetch the Button from an xml file in the project (I promise, the button exists, i didn't mispell the name, etc.) I output the button in the next line, it is null. I try to give it an onClickListener and it throws a null pointer exception. How is this button null? I just instantiated it the line before!

3 Answers 3

1

I just instantiated it the line before!

No you didn't,

newSubmitButton = (Button) findViewById(R.id.newPlayerSubmit)

does not instantiate anything. It simply retrieves the button from the active view. If the button isn't part of the active view (perhaps the layout hasn't been inflated yet?) then your button reference will be null. Are you calling this code in your Activity's onCreate() method? Have you called setContentView() before executing the code in question?

Sign up to request clarification or add additional context in comments.

Comments

1

Besides misspelling the name, you might have left out a call to setContentView().

(If that's not the issue, please post more code.)

Comments

1

Did you remember to setContentView() higher up? Just because R.java contains it doesn't mean it's attached to your view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.