java - NullPointerException onCreate - android spinner -
i'm having trouble spinner on application.
the spinner supposed pass selected item sqlite db , save data.
but far everytime press register button throws javanullpointerexception
error.
this class:
public class registermember extends activity implements onitemselectedlistener{ dbadapter dbadapter; edittext txtname; edittext txtpassword; edittext txtpasswordconf; edittext txtemail; edittext txtemailconf; edittext txtschool; button btnjoin; //button btnregister; private string[] state= {"usb","stanford","harvard","ucv","usm","tor vergata", "la sapienza"}; spinner spinner_school; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_members_register); system.out.println(state.length); spinner_school = (spinner) findviewbyid(r.id.spinner_school); arrayadapter<string> adapter_state = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, state); adapter_state .setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner_school.setadapter(adapter_state); spinner_school.setonitemselectedlistener(this); //btnregister = (button) findviewbyid(r.id.btn_reg); dbadapter = new dbadapter(this); dbadapter.open(); btnjoin.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { inputmethodmanager imm = (inputmethodmanager) getsystemservice(context.input_method_service); imm.hidesoftinputfromwindow(txtname.getwindowtoken(), 0); imm.hidesoftinputfromwindow(txtpassword.getwindowtoken(), 0); imm.hidesoftinputfromwindow(txtpasswordconf.getwindowtoken(), 0); imm.hidesoftinputfromwindow(txtemail.getwindowtoken(), 0); imm.hidesoftinputfromwindow(txtemailconf.getwindowtoken(), 0); //imm.hidesoftinputfromwindow(txtschool.getwindowtoken(), 0); //imm.hidesoftinputfromwindow(spinner_school.getwindowtoken(), 0); try { string name = txtname.gettext().tostring(); string password = txtpassword.gettext().tostring(); string passwordconf = txtpasswordconf.gettext().tostring(); string email = txtemail.gettext().tostring(); string emailconf = txtemailconf.gettext().tostring(); //string school = txtschool.gettext().tostring(); string school = spinner_school.getselecteditem().tostring(); long = dbadapter.register(name, password, email, school); if(i != -1) toast.maketext(registermember.this, "you have registered",toast.length_long).show(); else { if(!passwordconf.equals(password)) { toast.maketext(getapplicationcontext(), "password not matches", toast.length_long).show(); } if(!emailconf.equals(email)) { toast.maketext(getapplicationcontext(), "email not matches", toast.length_long).show(); } } } catch (sqlexception e) { toast.maketext(registermember.this, "some problem occurred", toast.length_long).show(); } } }); } public void onitemselected(adapterview<?> parent, view view, int position, long id) { spinner_school.setselection(position); } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }
the spinner on layout:
<tablerow android:id="@+id/tablerow10" android:layout_width="wrap_content" android:layout_height="wrap_content" > <spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinner_school" android:spinnermode="dropdown" /> </tablerow>
and stack trace, starts on method btnjoin.setonclicklistener(new onclicklistener()
java.lang.runtimeexception: unable start activity componentinfo{com.example.shairlook/com.kkoci.shairlook.registermember}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2245) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2295) @ android.app.activitythread.access$700(activitythread.java:150) @ android.app.activitythread$h.handlemessage(activitythread.java:1280) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:175) @ android.app.activitythread.main(activitythread.java:5279) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.kkoci.shairlook.registermember.oncreate(registermember.java:57)
any ideas? i've looked through examples still can't figure out this
thanks in advance!
edit on duplicate
no, duplicate not solve question because sqlite involved here, simple method issue, thanks.
second edit
this how btnjoin.setonclicklistener(new onclicklistener() {
method looks right now, same error:
btnjoin.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { inputmethodmanager imm = (inputmethodmanager) getsystemservice(context.input_method_service); imm.hidesoftinputfromwindow(txtname.getwindowtoken(), 0); imm.hidesoftinputfromwindow(txtpassword.getwindowtoken(), 0); imm.hidesoftinputfromwindow(txtpasswordconf.getwindowtoken(), 0); imm.hidesoftinputfromwindow(txtemail.getwindowtoken(), 0); imm.hidesoftinputfromwindow(txtemailconf.getwindowtoken(), 0); //imm.hidesoftinputfromwindow(txtschool.getwindowtoken(), 0); //imm.hidesoftinputfromwindow(spinner_school.getwindowtoken(), 0); try { txtname = (edittext) findviewbyid(r.id.et_user); string name = txtname.gettext().tostring(); txtpassword = (edittext) findviewbyid(r.id.et_pw_reg); string password = txtpassword.gettext().tostring(); txtpasswordconf = (edittext) findviewbyid(r.id.et_pw_conf); string passwordconf = txtpasswordconf.gettext().tostring(); txtemail = (edittext) findviewbyid(r.id.et_email_reg); string email = txtemail.gettext().tostring(); txtemailconf = (edittext) findviewbyid(r.id.et_email_conf); string emailconf = txtemailconf.gettext().tostring(); //string school = txtschool.gettext().tostring(); spinner_school = (spinner) findviewbyid(r.id.spinner_school); string school = spinner_school.getselecteditem().tostring(); long = dbadapter.register(name, password, email, school); if(i != -1) toast.maketext(registermember.this, "you have registered",toast.length_long).show(); else { if(!passwordconf.equals(password)) { toast.maketext(getapplicationcontext(), "password not matches", toast.length_long).show(); } if(!emailconf.equals(email)) { toast.maketext(getapplicationcontext(), "email not matches", toast.length_long).show(); } } } catch (sqlexception e) { toast.maketext(registermember.this, "some problem occurred", toast.length_long).show(); } } });
you haven't assigned value btnjoin
. causing exception.
edit 1 - how fix problem
to fix problem, , other problems happen if click button, add following lines oncreate() method after setcontentview(r.layout.activity_members_register)
:
txtname = (edittext)findviewbyid(r.id.register_member_name); txtpassword = (edittext)findviewbyid(r.id.register_member_password); txtpasswordconf = (edittext)findviewbyid(r.id.register_member_password_confirm); txtemail = (edittext)findviewbyid(r.id.register_member_email); txtemailconf = (edittext)findviewbyid(r.id.register_member_email_confirm); txtschool = (edittext)findviewbyid(r.id.register_member_school); btnjoin = (button)findviewbyid(r.id.register_member_button_join);
as long have on layout file:
... <edittext android:id="@+id/register_member_name" ... /> ... <edittext android:id="@+id/register_member_password" ... /> ... <edittext android:id="@+id/register_member_password_confirm" ... /> ... <edittext android:id="@+id/register_member_email" ... /> ... <edittext android:id="@+id/register_member_email_confirm" ... /> ... <edittext android:id="@+id/register_member_school" ... /> ... <button android:id="@+id/register_member_button_join" ... /> ...
where register_member_name
, register_member_password
, register_member_password_confirm
, register_member_email
, register_member_email_confirm
, register_member_school
, register_member_button_join
widgets ids. don't have use these names. long using same values in both layout file , java implementation, going fine.
Comments
Post a Comment