Search code examples
cursorandroid-sqliteindexoutofboundsexception

How to fix 'database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0'?


I am setting up a database with Android SQLite for my weight_activity with a dialog. In that Activity I save the current weight and I want to request the saved value for the current weight when starting the activity to display it.

The Error I get is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thisfit/com.example.thisfit.Weight_Activity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)

This is the code that requests the current weight in my activity class

    public void getCurrentWeightAsFloat() {
        textView_currentWeight.setText(String.valueOf(weight_dbHandler.getCurrentWeight()));
    } //public void getCurrentWeightAsFloat()

This is the whole database code. I post this because I don't know, if the problem could be in another section of the code and not just the method getCurrentWeight():

public class Weight_Database extends SQLiteOpenHelper {

    //region Attributes
    private static final String TAG = "weight_database";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "weight_database.db";
    private static final String TABLE_NAME = "weight_table";
    private static final String COL1 = "ID";
    private static final String COL2 = "Weight";
    private static final String COL3 = "Date";
    //endregion





    //region Constructors
    public Weight_Database(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
//        SQLiteDatabase db = this.getWritableDatabase();
    }
    //endregion





    //region Methods

    @Override
    public void onCreate(SQLiteDatabase db) {

        String createTable = "CREATE TABLE " + TABLE_NAME
                + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COL2 + " FLOAT, "
                + COL3 + " DATE )";
        db.execSQL(createTable);
    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }



    public boolean addNewCurrentWeightAsFloat(float newCurrentWeightAsFloat){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, newCurrentWeightAsFloat);

        Log.d(TAG, "addWeight: Adding " + newCurrentWeightAsFloat + " to " + TABLE_NAME);

        long result = db.insert(TABLE_NAME, null, contentValues);

        //if date as inserted incorrectly it will return -1
        if (result == -1){
            return false;
        } else {
            return true;
        }
    } //public boolean addNewCurrentWeightAsFloat(String newCurrentWeightAsFloat)



    public float getCurrentWeight(){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT Weight FROM " + TABLE_NAME + " ORDER BY ID DESC LIMIT 1";
        Cursor data = db.rawQuery(query, null);
        data.moveToFirst();

        float currentWeightAsFloat = data.getFloat(data.getColumnIndex("content"));

        return currentWeightAsFloat;
    } //public float getCurrentWeight()



    //Returns the whole table as a raw query
    public Cursor getData(){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_NAME;
        Cursor data = db.rawQuery(query, null);
        return data;
    } //public Cursor getData()


    //endregion





} //class Weight_Database

I expect to get the latest weight entered by selecting the ID DESCending with a Limit of 1. I want to get this float of my current weight and display it in a textView afterwards.

Have a nice day and thanks in advance :)


Solution

  • Your table has no data.

    You should check the return values of data.moveToFirst() and only try to call getFloat() on the cursor in case it returned true.