Reputation: 289
public class PlaceCallActivity extends BaseActivity {
private static final String SELECT_SQL = "SELECT * FROM numbers";
protected void onCreate(Bundle savedInstanceState) {
opendatabase();
c = db.rawQuery(SELECT_SQL, null);
c.moveToLast();
userNo.setText(c.getString(c.getColumnIndex("number")));
}
public void opendatabase(){
db=openOrCreateDatabase("NumberDB", Context.MODE_PRIVATE, null);
}
I want to set text of the last retrieve data from column of "number" At Runtime i recieved the following error :
CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
The error is at line where i am setting the text.
i.e. "userNo.setText(c.getString(c.getColumnIndex("number")));"
Upvotes: 1
Views: 67
Reputation: 2848
public class PlaceCallActivity extends BaseActivity {
private static final String SELECT_SQL = "SELECT * FROM numbers";
protected void onCreate(Bundle savedInstanceState) {
opendatabase();
c = db.rawQuery(SELECT_SQL, null);
if(c.getCount()>0)
{
c.moveToLast();
userNo.setText(c.getString(c.getColumnIndex("number")));
}
}
public void opendatabase(){
db=openOrCreateDatabase("NumberDB", Context.MODE_PRIVATE, null);
}
Upvotes: 0
Reputation: 56925
You are getting this error because there is no data in cursor, before fetching data from cursor you have to first check it's count.
if(c.getCount()!=0)
{
if(c.moveToLast())
{
}
}
Upvotes: 1