Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The issue is purely with the contents inside the tables. If the table is not empty (with one or more records), application works perfectly. I am deleting the contents of table and immediately after that reading the same table, it throws exception and app force closes.

I tried searching for it but couldn't conclude. The key point is : index out of bound exception which is thrown at movetofirst() method of cursor when i am going to read the table, i suppose... Please help.

public List<TableData> readForPaymentDetais()
    {
        List<TableData> paymentDetails = new ArrayList<TableData>();
        try
        {
            String selectQuery = "select * from PaymentDetails";
            SQLiteDatabase database = this.getWritableDatabase();
            Cursor cursor = database.rawQuery(selectQuery, null);
            if(cursor.getCount() !=0)
            {
                if(cursor.moveToFirst())
                {
                    do
                    {
                        TableData data = new TableData();
                        data.setPaymentMade(Float.valueOf(cursor.getString(0).toString()));
                        data.setDateOfPayment(cursor.getString(1));

                        paymentDetails.add(data);               
                    }
                    while(cursor.moveToNext());
                }               
            }

            return paymentDetails;
        }
        catch(Exception exc)
        {
            return null;
        }       
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
145 views
Welcome To Ask or Share your Answers For Others

1 Answer

Before executing moveToFirst method of cursor please check whether cursor is empty. For that you can use code like:

if (mCursor.getCount() == 0) {
    // cursor is empty
}

If cursor is not empty put your stuff in else part.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...