java - App crashes when writing to SQLite Database in Android -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i have developed app, user registration seems crash app. have checked cannot find cause, if can assist. code below.
java class
public class signup extends appcompatactivity { databasehelper helper = new databasehelper(this); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_sign_up); } public void onsignupclick (view view) { if (view.getid() == r.id.bsignup) { edittext name = (edittext)findviewbyid(r.id.tfusername); edittext email = (edittext)findviewbyid(r.id.tfemail); edittext uname = (edittext)findviewbyid(r.id.tfuname); edittext pass1 = (edittext)findviewbyid(r.id.tfpass1); //edittext pass2 = (edittext)findviewbyid(r.id.tfpass2); string namestr = name.gettext().tostring(); string emailstr = email.gettext().tostring(); string unamestr = uname.gettext().tostring(); string pass1str = pass1.gettext().tostring(); contact c = new contact(); c.setname(namestr); c.setemail(emailstr); c.setuname(unamestr); c.setpass(pass1str); helper.insertcontact(c); } } }
xml layout
<linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <imageview android:layout_width="match_parent" android:src ="@drawable/saica" android:layout_height="120dp" /> <textview android:text="name" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview2" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="" android:ems="10" android:id="@+id/tfname" /> <textview android:text="email address" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview3" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="" android:ems="10" android:id="@+id/tfemail" /> <textview android:text="username" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview4" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="" android:ems="10" android:id="@+id/tfuname" /> <textview android:text="password" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview5" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="" android:ems="10" android:id="@+id/tfpass1" android:password="true" /> <button android:text="register" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bsignup" android:onclick="onsignupclick"/> </linearlayout>
error log
fatal exception: main process: com.example.pooveshin.saica_sgb, pid: 10321 java.lang.illegalstateexception: not execute method android:onclick @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:293) @ android.view.view.performclick(view.java:4780) @ android.view.view$performclick.run(view.java:19866) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.text.editable android.widget.edittext.gettext()' on null object reference @ com.example.pooveshin.saica_sgb.signup.onsignupclick(signup.java:30) @ java.lang.reflect.method.invoke(native method)
you don't have edittext
named tfusername
, actual edittext id tfname
. should be
edittext name = (edittext)findviewbyid(r.id.tfname);
Comments
Post a Comment