java - android app crashes and gives error in registration -


i have in process of developing registration app using android studios, once users try register application says error in registration , logcat not give error, when debugging app, still no errors.

please can , tell me going wrong code correct working couple of months ago, since have returned holiday says "error occurred in registration".

user data gets entered database not allow user move onto next activity says "error occurred in registration". please can or advise?

logcat

  01-07 15:14:42.933 2161-4203/com.oakland e/json parser: error parsing data org.json.jsonexception: value 2016-01-07 of type java.lang.string cannot converted jsonobject 01-07 15:14:42.938 2161-2161/com.oakland e/androidruntime: fatal exception: main                                                                        process: com.oakland, pid: 2161                                                                        java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string org.json.jsonobject.getstring(java.lang.string)' on null object reference                                                                            @ com.oakland.register$processregister.onpostexecute(register.java:211)                                                                            @ com.oakland.register$processregister.onpostexecute(register.java:171) 

line 211- if (json.getstring(key_success) != null) {

register.java

@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) {         try {             if (json != null && json.getstring(key_success) != null) {                 if (json != null && 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 occurred in registration");             }          } catch (jsonexception e) {             e.printstacktrace();           }     }} public void netasync(view view){     new netcheck().execute(); }} 

userfunction.java

//url of php api      private static string registerurl = "http://10.0.2.2/register_api/";  private static string register_tag = "register";        /**          * 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;         } 

jsonpasar.java

public class jsonparser {      static inputstream = null;     static jsonobject jobj = null;     static string json = "";      // constructor     public jsonparser() {      }      public jsonobject getjsonfromurl(string url, list<namevaluepair> 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;      } } 

databasehandler.java

public class databasehandler extends sqliteopenhelper {      // static variables     // database version     private static final int database_version = 1;      // database name     private static final string database_name = "cloud_contacts";      // login table name     private static final string table_login = "login";      // login table columns names     private static final string key_id = "id";     private static final string key_firstname = "fname";     private static final string key_lastname = "lname";     private static final string key_email = "email";     private static final string key_username = "uname";     private static final string key_uid = "uid";     private static final string key_created_at = "created_at";      public databasehandler(context context) {         super(context, database_name, null, database_version);     }      // creating tables     @override     public void oncreate(sqlitedatabase db) {         string create_login_table = "create table " + table_login + "("                 + key_id + " integer primary key,"                 + key_firstname + " text,"                 + key_lastname + " text,"                 + key_email + " text unique,"                 + key_username + " text,"                 + key_uid + " text,"                 + key_created_at + " text" + ")";         db.execsql(create_login_table);     }      // upgrading database     @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         // drop older table if existed         db.execsql("drop table if exists " + table_login);          // create tables again         oncreate(db);     }      /**      * storing user details in database      * */     public void adduser(string fname, string lname, string email, string uname, string uid, string created_at) {         sqlitedatabase db = this.getwritabledatabase();          contentvalues values = new contentvalues();         values.put(key_firstname, fname); // firstname         values.put(key_lastname, lname); // lastname         values.put(key_email, email); // email         values.put(key_username, uname); // username         values.put(key_uid, uid); // email         values.put(key_created_at, created_at); // created @          // inserting row         db.insert(table_login, null, values);         db.close(); // closing database connection     }       /**      * getting user data database      * */     public hashmap<string, string> getuserdetails(){         hashmap<string,string> user = new hashmap<string,string>();         string selectquery = "select  * " + table_login;          sqlitedatabase db = this.getreadabledatabase();         cursor cursor = db.rawquery(selectquery, null);         // move first row         cursor.movetofirst();         if(cursor.getcount() > 0){             user.put("fname", cursor.getstring(1));             user.put("lname", cursor.getstring(2));             user.put("email", cursor.getstring(3));             user.put("uname", cursor.getstring(4));             user.put("uid", cursor.getstring(5));             user.put("created_at", cursor.getstring(6));         }         cursor.close();         db.close();         // return user         return user;     }           /**      * getting user login status      * return true if rows there in table      * */     public int getrowcount() {         string countquery = "select  * " + table_login;         sqlitedatabase db = this.getreadabledatabase();         cursor cursor = db.rawquery(countquery, null);         int rowcount = cursor.getcount();         db.close();         cursor.close();          // return row count         return rowcount;     }       /**      * re crate database      * delete tables , create them again      * */     public void resettables(){         sqlitedatabase db = this.getwritabledatabase();         // delete rows         db.delete(table_login, null, null);         db.close();     } 

i suggest use jackson create json strings. set object , convert json string. converting object easy.

error connected parsing error. hmm. that's weird. user added database if "error occurred in registration" shows, code adduser couldn't executed because it's inside if. don't application flow :( can tell more in comment? "registeruser" doesn't register user create json see.

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)); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -