java - JSON: Null pointer exception -
from this tutorial, got error
java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string org.json.jsonobject.tostring()' on null object reference
it came mainactivity.java
class attemptlogin extends asynctask<string, string, string> { // <--- cause of error /** * before starting background thread show progress dialog * */ boolean failure = false; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("attempting login..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected string doinbackground(string... args) { // todo auto-generated method stub // here check success tag int success; string username = user.gettext().tostring(); string password = pass.gettext().tostring(); try { list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("username", username)); params.add(new basicnamevaluepair("password", password)); log.d("request!", "starting"); jsonobject json = jsonparser.makehttprequest( login_url, "post", params); // checking log json response log.d("login attempt", json.tostring()); // <--- cause of error // success tag json success = json.getint(tag_success); if (success == 1) { log.d("successfully login!", json.tostring()); intent ii = new intent(mainactivity.this,otheractivity.class); startactivity(ii); // finish() method used tell android os done current // activity now! moving other activity finish(); return json.getstring(tag_message); } else{ return json.getstring(tag_message); } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * once background process done need dismiss progress dialog asap **/ protected void onpostexecute(string message) { pdialog.dismiss(); if (message != null){ toast.maketext(mainactivity.this, message, toast.length_long).show(); } } }
there's lots of people asking same question still no answer particular problem.
this first time using json, there's wrong in code , didn't found yet.
hope me figure out did wrong. thank you.
why not favor , use retrofit getting / parsing json.
here tutorials:
http://inthecheesefactory.com/blog/retrofit-2.0/en
https://github.com/codepath/android_guides/wiki/consuming-apis-with-retrofit
Comments
Post a Comment