• Register
0 votes
36 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

Please log in or register to answer this question.

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 23 views
23 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, 2020 ArifulIslam 7.5k points
0 votes
1 answer 8 views
8 views
Problem: Which of the following, with regard to the entire database, should be made on a regular basis?
asked Jul 22, 2020 sstones 830 points
0 votes
1 answer 13 views
13 views
Problem: index column size too large. the maximum column size is 767 bytes.
asked Feb 13 Shovo210 2.5k points
0 votes
1 answer 22 views
0 votes
1 answer 9 views
9 views
Cannot open database requested by the login I can not log in to a database in SQL Server. Can you suggest a solution?
asked Nov 11, 2020 miki 1.9k points
0 votes
1 answer 474 views
474 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 7 views
7 views
problem java.lang.illegalstateexception: already managing a googleapiclient with id 0
asked Feb 9 charles mathews 3.8k points
0 votes
1 answer 32 views
32 views
Problem: How can I access Oracle from Python? I have downloaded a cx_Oracle msi installer, but Python can't import the library. I get the following error: import cx_Oracle Traceback (most recent call last): File "<pyshell1>", line 1, in <module> import cx_Oracle ImportError: DLL load failed: The specified module could not be found.
asked Jan 22 Mashhoodch 9.1k points
0 votes
2 answers 25 views
25 views
Problem: What function does compacting an Access database perform other than reducing the file size?
asked Mar 23, 2020 ArifulIslam 7.5k points
0 votes
1 answer 5 views
5 views
Problem: could not resolve com.android.support:appcompat-v7:26.1.0.
asked 3 days ago Dan phillip 1.9k points