java - Getting number format exception in android -


i newbie android , have implemented class in data coming database, have used cursor gives me numberformatexception.

my code is:

package com.example.login;  import android.os.bundle; import android.app.activity; import android.content.context; import android.database.cursor; import android.database.databaseerrorhandler; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast;  public class mainactivity extends activity { button b,b1; edittext e1,e2,e3; sqlitedatabase a; database db; cursor r; string s=""; string d=""; string w,t; int q;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         b=(button)findviewbyid(r.id.button1);         e1=(edittext)findviewbyid(r.id.edittext1);         e2=(edittext)findviewbyid(r.id.edittext2);         e3=(edittext)findviewbyid(r.id.edittext3);         q=integer.parseint(e1.gettext()+"");         w=e2.gettext()+"";         t=e3.gettext()+"";          b1.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                 a=db.getwritabledatabase();                 a.execsql("insert data values("+q+",'"+w+"','"+t+"');");                 db.close();             }         });         b.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                 a=db.getreadabledatabase();                 r=a.rawquery("select name data", null);                 system.out.println("========cursor======"+r.tostring());                 if(r.getcount()>0){                     r.movetonext();                     s+=r.getstring(0);                 }                 s+=r.getstring(0);                 db.close();                 /*a=db.getreadabledatabase();                 r=a.rawquery("select pass data", null);                 d+=r.getstring(0);                 db.close();*/                 toast.maketext(getbasecontext(), s, toast.length_long).show();             }         });     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }  } class database extends sqliteopenhelper{      public database(context context) {         super(context, "data", null, 2);         // todo auto-generated constructor stub     }      @override     public void oncreate(sqlitedatabase arg0) {         // todo auto-generated method stub         arg0.execsql("create table data(cd int,name string ,pass string);");     }      @override     public void onupgrade(sqlitedatabase arg0, int arg1, int arg2) {         // todo auto-generated method stub         arg0.execsql("drop table data;");         oncreate(arg0);      }  } 

logcat

07-28 17:06:47.988: e/androidruntime(28379): process: com.example.login, pid: 28379 07-28 17:06:47.988: e/androidruntime(28379): java.lang.runtimeexception: unable start activity componentinfo{com.example.login/com.example.login.mainactivity}: java.lang.numberformatexception: invalid int: "" 07-28 17:06:47.988: e/androidruntime(28379):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2404) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2464) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.app.activitythread.access$900(activitythread.java:172) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.app.activitythread$h.handlemessage(activitythread.java:1308) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.os.handler.dispatchmessage(handler.java:102) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.os.looper.loop(looper.java:146) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.app.activitythread.main(activitythread.java:5653) 07-28 17:06:47.988: e/androidruntime(28379):    @ java.lang.reflect.method.invokenative(native method) 07-28 17:06:47.988: e/androidruntime(28379):    @ java.lang.reflect.method.invoke(method.java:515) 07-28 17:06:47.988: e/androidruntime(28379):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1291) 07-28 17:06:47.988: e/androidruntime(28379):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1107) 07-28 17:06:47.988: e/androidruntime(28379):    @ dalvik.system.nativestart.main(native method) 07-28 17:06:47.988: e/androidruntime(28379): caused by: java.lang.numberformatexception: invalid int: "" 07-28 17:06:47.988: e/androidruntime(28379):    @ java.lang.integer.invalidint(integer.java:137) 07-28 17:06:47.988: e/androidruntime(28379):    @ java.lang.integer.parseint(integer.java:358) 07-28 17:06:47.988: e/androidruntime(28379):    @ java.lang.integer.parseint(integer.java:331) 07-28 17:06:47.988: e/androidruntime(28379):    @ com.example.login.mainactivity.oncreate(mainactivity.java:36) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.app.activity.performcreate(activity.java:5541) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1093) 07-28 17:06:47.988: e/androidruntime(28379):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2368) 07-28 17:06:47.988: e/androidruntime(28379):    ... 11 more 

your exception is:

caused by: java.lang.numberformatexception: invalid int: ""

this means must ensure values parsed. "" (nothing) not number. set text initial value such as: e1.settext("42"); ensure correct parsing, or dont parse, if string null/empty.

note oncreate gives no chance user alter data. getting empty value.

if(e1.gettext()!=null && !e1.gettext().isempty()){    q=integer.parseint(e1.gettext()+""); } else {    log.v(yourtag,"no number parse, setting -1 instead");    q = -1; } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -