Android json parse and store into database -


i making app have database , trying store data using method , getting error javanullpointer,

jsonparser jparser = new jsonparser();                     jsonobject json = jparser.getjsonfromurl(url);                         // todo auto-generated method stub                           jsonarray erate= json.getjsonarray("erates");                         if(erate!=null)                         {                          // looping through contacts                         for(int = 0; < erate.length(); i++){                             jsonobject c = erate.getjsonobject(i);                             cursor cr = db.rawquery("select * `rates` `id`='"+c.getstring("id")+"'",null);                                  string query=                             "insert rates(id,kondisi,condition,tenor,erate,eratedb)"+                             "values("+                             c.getstring("id")+",'"+                              c.getstring("....")                             +"')";                                 db.execsql(query);                             log.d("insert",query);                              cr.close();}                         } 

and try method (this give same error)

          (int = 0; < erates.length(); i++) {                     jsonobject c = erates.getjsonobject(i);                 int id = c.getint(tag_er_id);                     int tenor = c.getint(tag_er_tenor);                     string ko = c.getstring(tag_er_k);                     string co = c.getstring(tag_er_c);                     double rate = c.getdouble(tag_er_rate);                     double ratedb = c.getdouble(tag_er_rate_db);                      log.d("response test: ", id + "\n" + tenor + "\n" + ko                             + "\n" + co + "\n" + rate + "\n" + ratedb);                     /**                      * updating parsed json data db                      * */                     db.saveentryeffectiverate(id, tenor,ko,co,                      rate,ratedb);                 } 

saveentryeffectiverate in database:

public void saveentryeffectiverate(int id, int tenor,string ko,string co, double rate,             double ratedb) {         sqlitedatabase db = this.getwritabledatabase();         contentvalues valueser = new contentvalues();         valueser.put(key_er_id, id);         valueser.put(key_er_tenor, tenor);         valueser.put(key_er_ko, ko);         valueser.put(key_er_co, co);         valueser.put(key_er_rate, rate);         valueser.put(key_er_rate_db, ratedb);         db.insert(table_effective_rate, null, valueser);     } 

update: logcat:

04-23 16:44:14.044: e/androidruntime(6752): fatal exception: main 04-23 16:44:14.044: e/androidruntime(6752): java.lang.runtimeexception: unable instantiate activity componentinfo{www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom/www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.mainactivity}: java.lang.nullpointerexception 04-23 16:44:14.044: e/androidruntime(6752):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1544) 04-23 16:44:14.044: e/androidruntime(6752):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:1638) 04-23 16:44:14.044: e/androidruntime(6752):     @ android.app.activitythread.access$1500(activitythread.java:117) 04-23 16:44:14.044: e/androidruntime(6752):     @ android.app.activitythread$h.handlemessage(activitythread.java:928) 04-23 16:44:14.044: e/androidruntime(6752):     @ android.os.handler.dispatchmessage(handler.java:99) 04-23 16:44:14.044: e/androidruntime(6752):     @ android.os.looper.loop(looper.java:123) 04-23 16:44:14.044: e/androidruntime(6752):     @ android.app.activitythread.main(activitythread.java:3647) 04-23 16:44:14.044: e/androidruntime(6752):     @ java.lang.reflect.method.invokenative(native method) 04-23 16:44:14.044: e/androidruntime(6752):     @ java.lang.reflect.method.invoke(method.java:507) 04-23 16:44:14.044: e/androidruntime(6752):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 04-23 16:44:14.044: e/androidruntime(6752):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 04-23 16:44:14.044: e/androidruntime(6752):     @ dalvik.system.nativestart.main(native method) 04-23 16:44:14.044: e/androidruntime(6752): caused by: java.lang.nullpointerexception 04-23 16:44:14.044: e/androidruntime(6752):     @ www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.mainactivity.<init>(mainactivity.java:55) 04-23 16:44:14.044: e/androidruntime(6752):     @ java.lang.class.newinstanceimpl(native method) 04-23 16:44:14.044: e/androidruntime(6752):     @ java.lang.class.newinstance(class.java:1409) 04-23 16:44:14.044: e/androidruntime(6752):     @ android.app.instrumentation.newactivity(instrumentation.java:1021) 04-23 16:44:14.044: e/androidruntime(6752):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1536) 04-23 16:44:14.044: e/androidruntime(6752):     ... 11 more 

update:db:

public class databasemaster2 extends sqliteopenhelper {     sqlitedatabase mydb;     // database version     private static final int database_version = 7;      // database name     private static final string database_name = "rates";      // table names     //effective_rate     public static final string key_er_id = "er_id";     public static final string key_er_tenor = "er_tenor";     public static final string key_er_ko = "er_ko";     public static final string key_er_co = "er_co";     public static final string key_er_rate = "er_rate";     public static final string key_er_rate_db = "er_rate_db" ;     //er_linker     public static final string key_er_l_id = "kondisi_id";     public static final string key_er_l_k = "kondisi";     public static final string key_er_l_c = "condition";     //as_linker     public static final string key_as_l_id = "asuransi_type_id";     public static final string key_as_l_ty = "asuransi_type";     //as_linker_zona     public static final string key_as_l_z_id = "aslz_id";     public static final string key_as_l_z_zona = "aslz_zona";     public static final string key_as_l_z_zone = "aslz_zone";     // asuransi rate     public static final string key_as_id = "as_id";     public static final string key_as_zo = "as_zona";     public static final string key_as_tenor = "as_tenor";     public static final string key_as_tlo = "tlo";     public static final string key_as_comprehensive = "comprehensive";     public static final string key_as_combine = "combine";     //the tables     public static final string table_effective_rate = "effective_rate";     public static final string table_asuransi_rate = "asuransi_rate";     public static final string table_as_linker = "as_linker";     public static final string table_as_l_zona= "as_l_zona";     public static final string table_er_linker = "er_linker";      public databasemaster2(context context) {         super(context, database_name, null, database_version);         // todo auto-generated constructor stub     }      @override     public void oncreate(sqlitedatabase db) {         // todo auto-generated method stub          try {             db.begintransaction();             db.execsql("create table " + table_er_linker + " ("+ key_er_l_k + " text not null," + key_er_l_c+ " text not null)");             db.execsql("create table " + table_as_linker + " ("+ key_as_l_ty + " text not null)");             db.execsql("create table " + table_as_l_zona + " ("+ key_as_l_z_zona + " text not null, " + key_as_l_z_zone + " text not null)");             db.execsql("create table " + table_effective_rate + " ("+ key_er_id + " integer primary key, " + key_er_tenor+" integer," +key_er_ko+" text not null, "+key_er_co+" text not null, "+key_er_rate+" real, "+ key_er_rate_db + " real)");             db.execsql("create table " + table_asuransi_rate + " ("+ key_as_id + " integer primary key, "+ key_as_zo + " integer, " + key_as_tenor+ " integer," + key_as_tlo + " real,"+ key_as_comprehensive + " real," + key_as_combine+ " real)");             // inserts pre-defined departments               db.settransactionsuccessful();         } {             db.endtransaction();         }      }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         // todo auto-generated method stub          db.execsql("drop table if exists " + table_effective_rate);         db.execsql("drop table if exists " + table_asuransi_rate);         db.execsql("drop table if exists " + table_er_linker);         db.execsql("drop table if exists " + table_as_linker);         db.execsql("drop table if exists " + table_as_l_zona);         oncreate(db);      }      public int updateentryeffectiverate(entryeffectiverate er) {         sqlitedatabase db = this.getwritabledatabase();          contentvalues values = new contentvalues();         values.put(key_er_id, er.geterid());         values.put(key_er_tenor, er.getertenor());         values.put(key_er_rate, er.geterrate());         values.put(key_er_rate_db, er.geterratedb());          // updating row         return db.update(table_effective_rate, values, key_er_id + " = ?",                 new string[] { string.valueof(er.geterid()) });     }      //     public void deleteentryeffectiverate(long er_id) {         sqlitedatabase db = this.getwritabledatabase();         db.delete(table_effective_rate, key_er_id + " = ?",                 new string[] { string.valueof(er_id) });     }        public void saveentryeffectiverate(int id, int tenor,string ko,string co, double rate,             double ratedb) {         sqlitedatabase db = this.getwritabledatabase();         contentvalues valueser = new contentvalues();         valueser.put(key_er_id, id);         valueser.put(key_er_tenor, tenor);         valueser.put(key_er_ko, ko);         valueser.put(key_er_co, co);         valueser.put(key_er_rate, rate);         valueser.put(key_er_rate_db, ratedb);         db.insert(table_effective_rate, null, valueser);     }      public void saveentryasuransirate(int asid,int aszo,int astenor,double tlo,double comp,double comb) {         sqlitedatabase db = this.getwritabledatabase();         contentvalues valuesas = new contentvalues();         valuesas.put(key_as_id, asid);         valuesas.put(key_as_zo, aszo);         valuesas.put(key_as_tenor, astenor);         valuesas.put(key_as_tlo, tlo);         valuesas.put(key_as_comprehensive, comp);         valuesas.put(key_as_combine, comb);         db.insert(table_asuransi_rate, null, valuesas);     }      public void savekondisi(string k,string c) {         sqlitedatabase db = this.getwritabledatabase();         contentvalues valuesk = new contentvalues();         valuesk.put(key_er_l_k,k );         valuesk.put(key_er_l_c, c);         db.insert(table_er_linker, null, valuesk);     }      public void savetipeasuransi(string ty) {         sqlitedatabase db = this.getwritabledatabase();         contentvalues valuesta = new contentvalues();         valuesta.put(key_as_l_ty, ty);         db.insert(table_as_linker, null, valuesta);     }      public void savezona(string za,string ze) {         sqlitedatabase db = this.getwritabledatabase();         contentvalues valueszo = new contentvalues();         valueszo.put(key_as_l_z_zona, za);         valueszo.put(key_as_l_z_zone, ze);         db.insert(table_as_l_zona, null, valueszo);     }       //     public void deleteentryasuransirate(long as_id) {         sqlitedatabase db = this.getwritabledatabase();         db.delete(table_asuransi_rate, key_as_id + " = ?",                 new string[] { string.valueof(as_id) });     } 

i declare database inside mainclass doing parsing , storing databasemaster2 db; database master name of database class extends sqliteopenhelper

i put log make sure parsing done. , is. suggestion storing data database?

first open database getwritable().... , try code

jsonobject json_give_field_1 = jsonarray_items.getjsonobject(0);     (int = 0; < jsonarray_items.length(); i++) {         jsonobject fields_value;          try {                        cv.put("databasefield", fields_value.getstring(field));                db.insert(database_table, null, cv);          } catch (jsonexception e) {             e.printstacktrace();         }      } 

after ,must close database db.close(); hope work you


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -