android - default value when checkbox is not clicked -


i writing app selection of output depends on checkbox selected. have 4 checkboxes choosing different filters.

private  string  x=null;  public void oncheckboxclicked(view view) {      checkbox check1 = (checkbox) findviewbyid(r.id.checkbox_ok);     checkbox check2 = (checkbox) findviewbyid(r.id.checkbox_nok);     checkbox check3 = (checkbox) findviewbyid(r.id.checkbox_check);     checkbox check4 = (checkbox) findviewbyid(r.id.checkbox_allok);     if (check1.ischecked()) {         x = "0000";         log.d(tag, x);     } else if (check2.ischecked()) {         x = "1111";         log.d(tag, x);     } else if (check3.ischecked()) {         x = "8888";         log.d(tag, x);     } else if (check4.ischecked()) {         x = "";         log.d(tag, x);     } else{x=""; } } 

how put default value if none of checkboxes clicked? tried initialising string x passes filter value app crashes when try display results without checkbox being checked.

option 1: initialize string default value

string x = "mydefaultvalue"; 

option 2: using else, if no checkbox ischecked, else

if (check1.ischecked()) {     x = "0000";     log.d(tag, x); } else if (check2.ischecked()) {     x = "1111";     log.d(tag, x); } else if (check3.ischecked()) {     x = "8888";     log.d(tag, x); } else if (check4.ischecked()) {     x = "";     log.d(tag, x); } else{     x = "default value";     log.d(tag, x); } 

the error got nullpointerexeption because initialise string null. using 1 of 2 options fix problem.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -