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

AndroidDatabase Cursor

Android Problem Overview


I am using custom adapter extending cursor adapter for displaying data in listview, to display particular phone number i have passed the id to a method in database class but it is showing

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

while placing debugger in the the method it is not going after the line

num = cursor.getString(cursor.getColumnIndex("ContactNumber"));

Can any one help me to solve it. This is the code:

public String getNumberFromId(int id) 
{
	String num;
	db= this.getReadableDatabase();
	Cursor cursor = db.query(scheduletable, new String[] { "ContactNumber" },"_id="+id, null, null, null, null);
	cursor.moveToFirst();
	num = cursor.getString(cursor.getColumnIndex("ContactNumber")); 
	cursor.close();
	db.close();
	return num;
}

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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();
}
    

Solution 3 - Android

First check this Condition before fetching data

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

Solution 4 - Android

Check the return value from moveToFirst(), before you try to read anything from the cursor. It looks as if no results are being returned.

Solution 5 - Android

a save schema to query Cursors is

// just one
Cursor cursor = db.query(...);
if (cursor != null) {
    if (cursor.moveToFirst()) {
        value = cursor.getSomething();
    }
    cursor.close();
}

// multiple columns
Cursor cursor = db.query(...);
if (cursor != null) {
    while (cursor.moveToNext()) {
        values.add(cursor.getSomething());
    }
    cursor.close();
}

Solution 6 - Android

In case people are still looking:

Instead of searching for "ContactNumber" try searching for Phone.NUMBER. The tutorial has the code with more details: http://developer.android.com/training/basics/intents/result.html

Solution 7 - Android

try to uninstall the app and then again test it... actually the sqlite db is created only once when the app is first install... so if you think your logic is current then reinstalling the app will do the trick for you. !!!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser181291View Question on Stackoverflow
Solution 1 - AndroidShubhayuView Answer on Stackoverflow
Solution 2 - AndroidngeshView Answer on Stackoverflow
Solution 3 - AndroidKhanView Answer on Stackoverflow
Solution 4 - AndroidGraham BorlandView Answer on Stackoverflow
Solution 5 - AndroidzaplView Answer on Stackoverflow
Solution 6 - AndroidPouton GeraldView Answer on Stackoverflow
Solution 7 - AndroidabhiView Answer on Stackoverflow