Skip to content Skip to sidebar Skip to footer

Null Pointer Exception Using Checkbox

I am trying to show Checkbox Status by showing Toast whenever user do Click on button for that i am using AlertDialog, but getting Null Pointer Exception, Complete Logcat: 07-12 06

Solution 1:

If you are inflating a layout for your dialog you need to use dialog object to initialize view

chkSubscribe = (CheckBox)dialog.findViewById(R.id.chkSubscribe);

You are getting NPE coz you have not initialized chkSubscribe

chkSubscribe = (CheckBox)findViewById(R.id.chkSubscribe) //Initialization fails

You are also dismissing the dialog in which case chkSubscribe will be null.

Note : You can findViewById of the current view hierarchy set to the activity.

Solution 2:

You are calling findViewById inside click, here it is not calling the findViewByid on the main activity class but the loginButton and loginButton does not have check box in it. you need to call this line "chkSubscribe = (CheckBox)findViewById(R.id.chkSubscribe); " in Activity and then reference it in the onClick method, remember any method you call on this object in onClick refer to loginButton not your activity

Post a Comment for "Null Pointer Exception Using Checkbox"