eljainc
eljainc

Reputation: 117

Android problem: null exception error when dereferencing CheckBox

I'm running into a strange problem.

I have a CheckBox defined in a resource file:

When trying to see if the checkbox is checked or not, I use the following code:

CheckBox cb1 = (CheckBox)findViewById(R.id.check1);

when I execute the following:

if (cb1.isChecked()) { }

I get a null exception error. I cannot figure out why this is happening.

Thanks for any help.

Upvotes: 0

Views: 357

Answers (1)

Chris Fei
Chris Fei

Reputation: 1317

cb1 is null, which means that findViewById() couldn't find the view whose ID you specified. Do you have a CheckBox in your layout file with android:id="@+id/check1"?

Upvotes: 1

Related Questions