java - null pointer exception android JSONobject -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i have null pointer exception android @ line
** if (json.getstring(key_success) != null) {
public class register extends activity { /** * json response node names. **/ @override public void onbackpressed() { intent bkr = new intent(this,login.class); startactivity(bkr); this.finish(); } private static string key_success = "success"; private static string key_uid = "uid"; private static string key_firstname = "fname"; private static string key_lastname = "lname"; private static string key_username = "uname"; private static string key_email = "email"; 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 inputpassword; edittext reinputpassword; button btnregister; textview registererrormsg; /** * called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { window window = this.getwindow(); window.addflags(windowmanager.layoutparams.flag_translucent_status); super.oncreate(savedinstancestate); setcontentview(r.layout.register); /** * defining layout items **/ inputfirstname = (edittext) findviewbyid(r.id.fname); inputlastname = (edittext) findviewbyid(r.id.lname); inputusername = (edittext) findviewbyid(r.id.uname); inputemail = (edittext) findviewbyid(r.id.email); inputpassword = (edittext) findviewbyid(r.id.pword); reinputpassword = (edittext) findviewbyid(r.id.rpword); btnregister = (button) findviewbyid(r.id.register); registererrormsg = (textview) findviewbyid(r.id.register_error);
/** * button switches login screen on clicked **/
button login = (button) findviewbyid(r.id.bktologin); 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. **/ btnregister.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("")) && ( !inputemail.gettext().tostring().equals("")) ) { if ( inputusername.gettext().tostring().length() > 4 ) { if(inputpassword.gettext().tostring().equals(reinputpassword.gettext().tostring())) netasync(view); else { toast.maketext(getapplicationcontext(), "passwords don't match, re-enter passwords carefully", toast.length_short).show(); } } 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 netcheck extends asynctask<string,string,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(); registererrormsg.settext("error in network connection"); } } } private class processregister extends asynctask<string,string,jsonobject> {
/** * defining process dialog **/ private progressdialog pdialog;
string email,password,fname,lname,uname; @override protected void onpreexecute() { super.onpreexecute(); inputusername = (edittext) findviewbyid(r.id.uname); inputpassword = (edittext) findviewbyid(r.id.pword); fname = inputfirstname.gettext().tostring(); lname = inputlastname.gettext().tostring(); email = inputemail.gettext().tostring(); uname= 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(fname, lname, email, uname, password); return json; } @override protected void onpostexecute(jsonobject json) { /** * checks success message. **/ try { if (json.getstring(key_success) != null) { registererrormsg.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"); registererrormsg.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(); registererrormsg.settext("user exists"); } else if (integer.parseint(red) ==3){ pdialog.dismiss(); registererrormsg.settext("invalid email id"); } } else{ pdialog.dismiss(); registererrormsg.settext("error occured in registration"); } } catch (jsonexception e) { e.printstacktrace(); } }} public void netasync(view view){ new netcheck().execute(); }}
nullpointerexception
on line means json
object equal null. u need check if doesn't equal null before trying value it.
Comments
Post a Comment