android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 database database

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0


Whenever you are dealing with Cursors, ALWAYS check for null and check for moveToFirst() without fail.

if( cursor != null && cursor.moveToFirst() ){    num = cursor.getString(cursor.getColumnIndex("ContactNumber"));    cursor.close(); }

Place logs appropriately to see whether it is returning null or an empty cursor. According to that check your query.

Update Put both the checks in a single statement as mentioned by Jon in the comment below.

Update 2 Put the close() call within the valid cursor scope.


try this.. this will avoid an Exception being thrown when the cursor is empty..

if(cursor != null && cursor.moveToFirst()){    num = cursor.getString(cursor.getColumnIndex("ContactNumber"));     cursor.close();}


First check this Condition before fetching data

if(cursor!=null && cursor.getCount()>0){  cursor.moveToFirst();  num = cursor.getString(cursor.getColumnIndex("ContactNumber")); }