• Register
0 votes
32 views

Problem :

I am trying to use the custom adapter extending cursor adapter for displaying data in the listview, So that I can display particular phone number that I have passed the id to a method in the database class however it is giving following error :

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

while I was trying to place the debugger in the method it is not going after the below line

mynum = mycursor.getString(mycursor.getColumnIndex("ContactNumber"));

Below is my code:

public String getNumberFromId(int my id) 
{
    String mynum;
    mydb= this.getReadableDatabase();
    Cursor mycursor = mydb.query(scheduletable, new String[] { "ContactNumber" },"_id="+myid, null, null, null, null);
    mycursor.moveToFirst();
    mynum = mycursor.getString(mycursor.getColumnIndex("ContactNumber")); 
    mycursor.close();
    mydb.close();
    return mynum;
}

 

Can someone help me to resolve my issue?

6 5 3
7,540 points

1 Answer

0 votes

Solution :

Please make sure whenever you want to deal with the Cursors, to have the check for null and also check for moveToFirst() without fail as shown below :

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

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

Please have both the checks in a single statement as mentioned in the example above.

Also always put the close() call within the valid cursor scope.

9 7 4
38,600 points

Related questions

0 votes
2 answers 15 views
15 views
Problem: Android obesity is associated with a higher incidence of all the following except A) heart disease. B) diabetes mellitus. C) hypertension. D) cancer.
asked Apr 12 ArifulIslam 5.7k points
0 votes
1 answer 7 views
7 views
Problem: Which of the following, with regard to the entire database, should be made on a regular basis?
asked Jul 22 sstones 830 points
0 votes
1 answer 65 views
65 views
Problem: I am trying to find out if anyone is able to accomplish sending the multipart/form-data POST in the Android with the use of Volley yet? I tried doing it but no success in trying to upload my image/png using the POST request to my one of the server and so I am very curious if anyone has.
asked Aug 18 Raphael Pacheco 4.9k points
0 votes
1 answer 1.1K views
1.1K views
Problem : I am trying to learn the Android and Ionic. If I run the ionic build android command in the root of the ionic project, I am facing below error: FAILURE: Build failed with an exception. Where: Script '/home/javad/Desktop/javadApp/platforms/android/CordovaLib/cordova. ... Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
asked Feb 18 mphil 2.3k points
0 votes
2 answers 18 views
18 views
Problem: What function does compacting an Access database perform other than reducing the file size?
asked Mar 23 ArifulIslam 5.7k points