this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i have forget password activity connects mysql database , generates new password. however; json object returning null pointer exception. php script has html form testing, , it's functioning error sure in java code.
here's json parser class:
package test.example.com.test; import android.util.log; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.utils.urlencodedutils; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.util.list; public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if(method == "post"){ // request method post // 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(); }else if(method == "get"){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); 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(); } 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; } }
here's asynctask:
class forgetpass extends asynctask<string, string, string> { boolean failure = false; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(forg1.this); pdialog.settitle("generating new password..."); pdialog.setcancelable(true); pdialog.setindeterminate(false); pdialog.show(); } @override protected string doinbackground(string... args) { // todo auto-generated method stub // check success tag int success; string email; email = etmailfrpass.gettext().tostring(); try { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("email", email)); jsonobject json5 = js.makehttprequest( forget_url, "post", params); // log.d("ibrahim forget pass", "ibrahim forget pass"); success = json5.getint(tag_success); if (success == 1) { //log.d("password generated", json5.tostring()); pdialog.dismiss(); return json5.getstring(tag_message); } else { // log.d("generation failure!", json5.getstring(tag_message)); return json5.getstring(tag_message); } } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url1) { // dismiss dialog once product deleted pdialog.dismiss(); if (file_url1 != null) { toast.maketext(forg1.this, file_url1, toast.length_long).show(); } } }
here's log, error @ json.getint
05-08 11:12:28.336 16229-16554/test.example.com.test e/json parser﹕ error parsing data org.json.jsonexception: value fetet of type java.lang.string cannot converted jsonobject 05-08 11:12:28.336 16229-16554/test.example.com.test w/dalvikvm﹕ threadid=11: thread exiting uncaught exception (group=0x40aa5228) 05-08 11:12:28.346 16229-16554/test.example.com.test e/androidruntime﹕ fatal exception: asynctask #1 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:278) @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) @ java.util.concurrent.futuretask.setexception(futuretask.java:124) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) @ java.util.concurrent.futuretask.run(futuretask.java:137) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:208) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) @ java.lang.thread.run(thread.java:864) caused by: java.lang.nullpointerexception @ test.example.com.test.forg1$forgetpass.doinbackground(forg1.java:107) @ test.example.com.test.forg1$forgetpass.doinbackground(forg1.java:73) @ android.os.asynctask$2.call(asynctask.java:264) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) at java.util.concurrent.futuretask.run(futuretask.java:137) at android.os.asynctask$serialexecutor$1.run(asynctask.java:208) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) at java.lang.thread.run(thread.java:864)
assume went wrong part returns null in jobj
:
// 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;
now asynctask
tries call getstring()
method in returned jobj
null (whenever success 1 or not):
if (success == 1) { //log.d("password generated", json5.tostring()); pdialog.dismiss(); return json5.getstring(tag_message); } else { // log.d("generation failure!", json5.getstring(tag_message)); return json5.getstring(tag_message); }
and here go npe.
Comments
Post a Comment