php - Android java.lang.NullPointerException: lock == null -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i got following error. java.lang.nullpointerexception: lock == null while json file server.i googled cant find solution this. using wamp server, mysql , php connect database, when try register below error.
i have highlighted both of error on line 189 , 232 in block quotes. please me wrong these lines.
error log
-04 19:41:08.572 2414-2481/com.brad.visor e/buffer error﹕ error converting result java.lang.nullpointerexception: lock == null 07-04 19:41:08.572 2414-2481/com.brad.visor e/json parser﹕ error parsing data org.json.jsonexception: end of input @ character 0 of 07-04 19:41:08.602 2414-2414/com.brad.visor e/androidruntime﹕ fatal exception: main process: com.brad.visor, pid: 2414 java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string org.json.jsonobject.getstring(java.lang.string)' on null object reference @ com.brad.visor.register$processregister.onpostexecute(register.java:232) @ com.brad.visor.register$processregister.onpostexecute(register.java:189) @ android.os.asynctask.finish(asynctask.java:636) @ android.os.asynctask.access$500(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:653) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5257) @ 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)
register.java file
public class register extends activity { /** * json response node names. **/ 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; button btnregister; textview registererrormsg; /** * called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { 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); 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 ){ netasync(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 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> { // error here
/** * 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) { //error here
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(); }}
manifest.java file
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.brad.visor" > <application android:icon="@mipmap/logo" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".login"> </activity> <activity android:name=".register"> </activity> <activity android:name=".registered"> </activity> <activity android:name=".main"> </activity> <activity android:name=".passwordreset"> </activity> <activity android:name=".changepassword"> </activity> </application> <!-- allow connect internet , know current network state--> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.access_network_state" /> </manifest>
userfunction.java code
public class userfunctions { private jsonparser jsonparser; //url of php api private static string loginurl = "http://192.168.0.3:8080/bradvisor_login_api/"; private static string registerurl = "http://192.168.0.3:8080/bradvisor_login_api/"; private static string forpassurl = "http://192.168.0.3:8080/bradvisor_login_api/"; private static string chgpassurl = "http://192.168.0.3:8080/bradvisor_login_api/"; private static string login_tag = "login"; private static string register_tag = "register"; private static string forpass_tag = "forpass"; private static string chgpass_tag = "chgpass"; // constructor public userfunctions(){ jsonparser = new jsonparser(); } /** * function login **/ public jsonobject loginuser(string email, string password){ // building parameters list params = new arraylist(); params.add(new basicnamevaluepair("tag", login_tag)); params.add(new basicnamevaluepair("email", email)); params.add(new basicnamevaluepair("password", password)); jsonobject json = jsonparser.getjsonfromurl(loginurl, params); return json; } /** * function change password **/ public jsonobject chgpass(string newpas, string email){ list params = new arraylist(); params.add(new basicnamevaluepair("tag", chgpass_tag)); params.add(new basicnamevaluepair("newpas", newpas)); params.add(new basicnamevaluepair("email", email)); jsonobject json = jsonparser.getjsonfromurl(chgpassurl, params); return json; } /** * function reset password **/ public jsonobject forpass(string forgotpassword){ list params = new arraylist(); params.add(new basicnamevaluepair("tag", forpass_tag)); params.add(new basicnamevaluepair("forgotpassword", forgotpassword)); jsonobject json = jsonparser.getjsonfromurl(forpassurl, params); return json; } /** * function register **/ public jsonobject registeruser(string fname, string lname, string email, string uname, string password){ // building parameters list params = new arraylist(); params.add(new basicnamevaluepair("tag", register_tag)); params.add(new basicnamevaluepair("fname", fname)); params.add(new basicnamevaluepair("lname", lname)); params.add(new basicnamevaluepair("email", email)); params.add(new basicnamevaluepair("uname", uname)); params.add(new basicnamevaluepair("password", password)); jsonobject json = jsonparser.getjsonfromurl(registerurl,params); return json; } /** * function logout user * resets temporary data stored in sqlite database * */ public boolean logoutuser(context context){ databasehandler db = new databasehandler(context); db.resettables(); return true; } }
jsonpraser.java file
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } public jsonobject getjsonfromurl(string url, list params) { // making http request try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "n"); } is.close(); json = sb.tostring(); log.e("json", json); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; }
the problem seems doinbackground
returning null
. thus, when onpostexecute
runs, argument json
null
. avoid exception, test whether json
null
before proceeding rest of logic. you'll have find out why userfunction.registeruser(...)
returning null
fix problem.
Comments
Post a Comment