android - ' android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0'
the Error was because my DB SQlite empty because every time i run the App it's make a new DB for me ...
solution
so I deleted all my DB SQLite and I put condition,
my condition is " if there is data in my DB SQLite don't take data from internt"
My MainActivity
if(mySQL.checktitle (storyObject.getTitle().toString())) {
Log.d("--------------------", "data is exist ");
break;
}
My SQLiteOpenHelper class
public boolean checktitle ( String title){
SQLiteDatabase db= this.getWritableDatabase();
Cursor cur = db.rawQuery("SELECT * FROM stories where story_title='"+title+"'" ,null);
if(cur.getCount()>0)
return true;
return false;
}
Error-CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
This means, that Cursor
is either null or it is empty i.e 0 rows
in it.
Hitlist getPerson(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(
TABLE_NAME,
new String[] { COLUMN_ID, COLUMN_NAME, COLUMN_REASON, COLUMN_STATUS,
COLUMN_FATE, COLUMN_HOUSE, COLUMN_EXTRA, COLUMN_IMG },
COLUMN_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
//Edit Part
if (cursor != null && cursor.getCount()>0) {//perform operations on cursor only when
//it is neither Null nor empty
//another thing, you should check if moveToFirst() returns something or not
Hitlist person;
if (cursor.moveToFirst()) { //now here fetch things from cursor
boolean dead=(Integer.parseInt(cursor.getString(3))==0)?false:true;
person = new Hitlist(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), dead,
cursor.getString(4), cursor.getString(5),
cursor.getString(6), cursor.getString(7));
cursor.close();
return person;
} else {
cursor.close();
//print some message via Snackbar or Toast
}
} else { throw new Exception(); }
cursor.close();
throw new Exception();
}
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 error in Android
Suspecting your cursor not to be null even when there is no record in your database.......
Replace
if (cursor != null)
by
if (cursor != null && cursor.getCount() != 0)
How to fix 'database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0'?
Your table has no data.
You should check the return values of data.moveToFirst()
and only try to call getFloat()
on the cursor in case it returned true.
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 Error
you should check before trying to access to the cursor
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToNext() && cursor.getCount()>0) {
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
}
cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
if (cursor.moveToNext() && cursor.getCount()>0) {
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}
return path;
}
Related Topics
Will Google Android Ever Support .Net
Navigation Drawer Semi-Transparent Over Status Bar Not Working
Trying to Get the Display Size of an Image in an Imageview
Actionbar Logo Centered and Action Items on Sides
Error :: Duplicate Files During Packaging of APK
Pathpattern to Match File Extension Does Not Work If a Period Exists Elsewhere in the File Name
Change Color of the Overflow Button on Actionbar
Make an Android Button Change Background on Click Through Xml
Proguard: Can't Find Referenced Class Com.Google.Android.Gms.R
Replace Default Android Maps API V2 Change Mylocation Icon
How to Have a Transparent Imagebutton: Android
Creating a Softkeyboard with Multiple/Alternate Characters Per Key
Detect When Recyclerview Reaches the Bottom Most Position While Scrolling
"Canvas: Trying to Draw Too Large Bitmap" When Android N Display Size Set Larger Than Small