java - ERROR:CursorIndexOutOfBoundsException Index 0 requested, with a size of 0 -


here trying enter details follows , needs retrieved.

package com.example.orderform;  import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.content.sharedpreferences; import android.content.dialoginterface.onclicklistener; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.view; import android.widget.edittext; import android.widget.toast;  public class newactivity extends activity{    public void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_new);   }   public void onclick(view v) {     cursor c1=null;      final sqlitedatabase db;      db=openorcreatedatabase("order", mode_private, null);      db.execsql("create table if not exists codeo(name varchar,address varchar,code int(10),password int(10),phno int(10),email varchar,ccode int(10));");      final string name;     final string address;     final string code;     final string ccode;     final string password;     final string phno;     final string email;     final edittext n=(edittext) findviewbyid(r.id.edittext1);     final edittext c2=(edittext) findviewbyid(r.id.edittext2);     final edittext a=(edittext) findviewbyid(r.id.edittext3);     final edittext em=(edittext) findviewbyid(r.id.edittext4);     final edittext ph=(edittext) findviewbyid(r.id.edittext5);     final edittext cc=(edittext) findviewbyid(r.id.edittext6);     final edittext pass=(edittext) findviewbyid(r.id.edittext7);      name=n.gettext().tostring();     code=c2.gettext().tostring();     address=a.gettext().tostring();     phno=ph.gettext().tostring();     email=em.gettext().tostring();     ccode=cc.gettext().tostring();     password=pass.gettext().tostring();     if(name.equalsignorecase("")||code.equalsignorecase("")||ccode.equalsignorecase("")||password.equalsignorecase("")||address.equalsignorecase("")||phno.equalsignorecase("")||email.equalsignorecase(""))     {       toast.maketext(getapplicationcontext(), "no field can left blank", toast.length_long).show();     }     else     {       if(code.equals(ccode))       {         try         {            c1=db.rawquery("select * codeo code='"+code+"'", null);           c1.movetofirst();            if(c1.getcount()<=0)           {             try             {               alertdialog.builder alrt=new alertdialog.builder(newactivity.this);               alrt.settitle("confirmation");               alrt.setcancelable(false);               alrt.setmessage("are sure no more changes on above details");               alrt.setpositivebutton("yes",new onclicklistener() {                  public void onclick(dialoginterface dialog, int which) {                   // todo auto-generated method stub                   try               {                 db.execsql("insert codeo values('"+name+"','"+code+"','"+ccode+"','"+address+"','"+phno+"','"+email+"','"+password+"');");                 n.settext("");                 c2.settext("");                 pass.settext("");                 a.settext("");                 ph.settext("");                 em.settext("");                 cc.settext("");                 sharedpreferences sp= getsharedpreferences("my code",0);                 sharedpreferences.editor e=sp.edit();                 e.putstring("code1",code);                 /*e.putstring("address",address);                   e.putstring("name",name);                   e.putstring("email", email);                   e.putstring("ccode",ccode);                   e.putstring("phno",phno);                   e.putstring("password",password);                   e.putstring("code",code);*/                 e.commit();                 intent j=new intent(getbasecontext(), new1activity.class);                 startactivity(j);               }               catch(exception e)               {                 toast.maketext(getapplicationcontext(), "exception", toast.length_short).show();               }                 }               });               alrt.setnegativebutton("no", new dialoginterface.onclicklistener() {                  public void onclick(dialoginterface dialog, int which) {                   // todo auto-generated method stub                   dialog.cancel();                 }               });               alertdialog al=   alrt.create();               al.show();              }             catch(exception er)             {               toast.maketext(getapplicationcontext(), "exception in getcount", toast.length_short).show();             }           }           else           {             toast.maketext(getapplicationcontext(), "code taken", toast.length_short).show();           }          }         catch(exception er)         {           toast.maketext(getapplicationcontext(), "exception in isfirst", toast.length_short).show();         }       }       else       {         toast.maketext(getapplicationcontext(), "code , confirm code doesn't match", toast.length_long).show();         c2.settext("");         cc.settext("");       }     }   } } 

now trying retrieve data.but getting cursorindexoutofboundsexception index 0 requested, size of 0.

package com.example.orderform;  import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.view; import android.widget.textview;  public class new1activity extends activity {    @override   public  void oncreate(bundle savedinstancestate)   {      super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_new1);       string name,address,code,phno,email,ccode,password;     cursor c;      sqlitedatabase db;      sharedpreferences sp=getsharedpreferences("newactivity", 0);      name=sp.getstring("name", "");         code=sp.getstring("code", "");         address=sp.getstring("address", "");        phno=sp.getstring("phno", "");      email=sp.getstring("email","");      ccode=sp.getstring("ccode", "");      password=sp.getstring("password", "");      db=openorcreatedatabase("order", mode_multi_process, null);       db.execsql("create table if not exists codeo(name varchar,address varchar,code         int(10),password int(10),phno int(10),email varchar,ccode int(10));");      c=db.rawquery("select * codeo code='"+code+"'", null);       c.movetofirst();     name=c.getstring(c.getcolumnindex("name"));      code=c.getstring(c.getcolumnindex("code"));      address=c.getstring(c.getcolumnindex("address"));      phno=c.getstring(c.getcolumnindex("phno"));     email=c.getstring(c.getcolumnindex("email"));     ccode=c.getstring(c.getcolumnindex("ccode"));     password=c.getstring(c.getcolumnindex("password"));         textview n=(textview) findviewbyid(r.id.textview6);      n.settext(name);      textview c2=(textview) findviewbyid(r.id.textview7);      c2.settext(code);      textview a=(textview) findviewbyid(r.id.textview8);      a.settext(address);      textview em=(textview) findviewbyid(r.id.textview9);      em.settext(email);      textview ph=(textview) findviewbyid(r.id.textview10);      ph.settext(phno);      textview cc=(textview) findviewbyid(r.id.textview12);      cc.settext(ccode);      textview pass=(textview) findviewbyid(r.id.textview14);      pass.settext(password);    }   public void onclick(view view)   {     startactivity(new intent(this,codeactivity.class));   }   } 

problem :- cursor array not contain data in case, in other words `empty` 

you cant request index o if size of

cursor array 0 

as if remember

indexes start 0 in array.. 

so when you request 0 index means requesting first element of cursor array.

which further means cursor array contains @ least 1 element @ index zero.

which further derives size of cursor array not zero in case.

so need add condition in when requesting cursor array if size 0 should skip condition

refer image

when request index 0 here in image will 23.

but if size of cursor array zero cant request 0 index..

here size of array 6

and indexes of array in range of 0-5

array indexing


Comments