android application crashing due to java.lang.NullPointerException -
i following andorid development guide , seem getting errror while trying register account, aware of issues , location (line 197) cannot seem fix issue please find below copy of logcat , register code, if there anymore information need please comment , try , help.
04-24 21:24:32.735: e/androidruntime(30368): fatal exception: main 04-24 21:24:32.735: e/androidruntime(30368): java.lang.nullpointerexception 04-24 21:24:32.735: e/androidruntime(30368): @ com.loggedin.register$processregister.onpostexecute(register.java:197) 04-24 21:24:32.735: e/androidruntime(30368): @ com.loggedin.register$processregister.onpostexecute(register.java:1) 04-24 21:24:32.735: e/androidruntime(30368): @ android.os.asynctask.finish(asynctask.java:631) 04-24 21:24:32.735: e/androidruntime(30368): @ android.os.asynctask.access$600(asynctask.java:177) 04-24 21:24:32.735: e/androidruntime(30368): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) 04-24 21:24:32.735: e/androidruntime(30368): @ android.os.handler.dispatchmessage(handler.java:99) 04-24 21:24:32.735: e/androidruntime(30368): @ android.os.looper.loop(looper.java:176) 04-24 21:24:32.735: e/androidruntime(30368): @ android.app.activitythread.main(activitythread.java:5419) 04-24 21:24:32.735: e/androidruntime(30368): @ java.lang.reflect.method.invokenative(native method) 04-24 21:24:32.735: e/androidruntime(30368): @ java.lang.reflect.method.invoke(method.java:525) 04-24 21:24:32.735: e/androidruntime(30368): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1046) 04-24 21:24:32.735: e/androidruntime(30368): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:862) 04-24 21:24:32.735: e/androidruntime(30368): @ dalvik.system.nativestart.main(native method) 04-24 21:25:05.435: i/process(30368): sending signal. pid: 30368 sig: 9
register java
import android.app.activity; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.asynctask; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import com.loggedin.internal.databasehandler; import com.loggedin.internal.userfunctions; import org.json.jsonexception; import org.json.jsonobject; import java.io.ioexception; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; public class register extends activity { /** * json response node names. **/ private static string key_success = "success"; private static string key_uid = "id"; private static string key_firstname = "firstname"; private static string key_lastname = "lastname"; private static string key_username = "username"; private static string key_email = "email"; private static string key_dob = "dob"; private static string key_created_at = "created_at"; private static string key_error = "error"; /** * defining layout items. **/ edittext inputfirstname; edittext inputlastname; edittext inputusername; edittext inputemail; edittext inputdob; edittext inputpassword; button registerbtn; textview register_error; /** * called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register); /** * defining layout items **/ inputfirstname = (edittext) findviewbyid(r.id.firstname); inputlastname = (edittext) findviewbyid(r.id.lastname); inputusername = (edittext) findviewbyid(r.id.username); inputemail = (edittext) findviewbyid(r.id.email); inputdob = (edittext) findviewbyid(r.id.dob); inputpassword = (edittext) findviewbyid(r.id.password); registerbtn = (button) findviewbyid(r.id.registerbtn); register_error = (textview) findviewbyid(r.id.register_error); /** * button switches login screen on clicked **/ button login = (button) findviewbyid(r.id.bktologinbtn); login.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent myintent = new intent(view.getcontext(), login.class); startactivityforresult(myintent, 0); finish(); } }); /** * register button click event. * toast set alert when fields empty. * toast set alert username must 5 characters. **/ registerbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if ( ( !inputusername.gettext().tostring().equals("")) && ( !inputpassword.gettext().tostring().equals("")) && ( !inputfirstname.gettext().tostring().equals("")) && ( !inputlastname.gettext().tostring().equals("")) && ( !inputdob.gettext().tostring().equals("")) && ( !inputemail.gettext().tostring().equals("")) ) { if ( inputusername.gettext().tostring().length() > 5 ){ internetasync(view); } else { toast.maketext(getapplicationcontext(), "username should minimum 5 characters", toast.length_short).show(); } } else { toast.maketext(getapplicationcontext(), "one or more fields empty", toast.length_short).show(); } } }); } /** * async task check whether internet connection working **/ private class internetcheck extends asynctask<string, boolean, boolean> { private progressdialog ndialog; @override protected void onpreexecute(){ super.onpreexecute(); ndialog = new progressdialog(register.this); ndialog.setmessage("loading.."); ndialog.settitle("checking network"); ndialog.setindeterminate(false); ndialog.setcancelable(true); ndialog.show(); } @override protected boolean doinbackground(string... args){ /** * gets current device state , checks working internet connection trying google. **/ connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnected()) { try { url url = new url("http://www.google.com"); httpurlconnection urlc = (httpurlconnection) url.openconnection(); urlc.setconnecttimeout(3000); urlc.connect(); if (urlc.getresponsecode() == 200) { return true; } } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return false; } @override protected void onpostexecute(boolean th){ if(th == true){ ndialog.dismiss(); new processregister().execute(); } else{ ndialog.dismiss(); register_error.settext("error in network connection"); } } } private class processregister extends asynctask <string, string, jsonobject>{ /** * defining process dialog **/ private progressdialog pdialog; string email,password,firstname,lastname,dob,username; @override protected void onpreexecute() { super.onpreexecute(); inputusername = (edittext) findviewbyid(r.id.username); inputpassword = (edittext) findviewbyid(r.id.password); firstname = inputfirstname.gettext().tostring(); lastname = inputlastname.gettext().tostring(); email = inputemail.gettext().tostring(); dob = inputdob.gettext().tostring(); username= inputusername.gettext().tostring(); password = inputpassword.gettext().tostring(); pdialog = new progressdialog(register.this); pdialog.settitle("contacting servers"); pdialog.setmessage("registering ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected jsonobject doinbackground(string... args) { userfunctions userfunction = new userfunctions(); jsonobject json = userfunction.registeruser(firstname, lastname, dob, email, username, password); return json; } @override protected void onpostexecute(jsonobject json) { /** * checks success message. **/ if (json ==null){ } try { if (json.getstring(key_success) != null) { register_error.settext(""); string res = json.getstring(key_success); string red = json.getstring(key_error); if(integer.parseint(res) == 1){ pdialog.settitle("getting data"); pdialog.setmessage("loading info"); register_error.settext("successfully registered"); databasehandler db = new databasehandler(getapplicationcontext()); jsonobject json_user = json.getjsonobject("user"); /** * removes previous data in sqlite database **/ userfunctions logout = new userfunctions(); logout.logoutuser(getapplicationcontext()); db.adduser(json_user.getstring(key_firstname),json_user.getstring(key_lastname),json_user.getstring(key_email),json_user.getstring(key_username),json_user.getstring(key_uid),json_user.getstring(key_created_at)); /** * stores registered data in sqlite database * launch registered screen **/ intent registered = new intent(getapplicationcontext(), registered.class); /** * close views before launching registered screen **/ registered.addflags(intent.flag_activity_clear_top); pdialog.dismiss(); startactivity(registered); finish(); } else if (integer.parseint(red) ==2){ pdialog.dismiss(); register_error.settext("user exists"); } else if (integer.parseint(red) ==3){ pdialog.dismiss(); register_error.settext("invalid email id"); } } else{ pdialog.dismiss(); register_error.settext("error occured in registration"); } } catch (jsonexception e) { e.printstacktrace(); } } } public void internetasync(view view){ new internetcheck().execute(); } }
i can see 3 possibilities:
if (json ==null){ }
this doing nothing , json null ,
json.getstring(key_success)
could potentially culprit.
possibility pdialog or register_error. make sure check every resource before referencing, can see guessing 1 is. in fact npe 1 of common exceptions, please make sure validate data before using it, save headaches.
solution:
if (json ==null){ return; //you can potentially log error too, if helps. }
or
if(json != null) { try{ if (json.getstring(key_success) != null) { //... }
also, add checks for:
if(pdialog != null) { //manipulate element want. }
and
if(register_error != null) { //manipulate element want. }
Comments
Post a Comment