ERROR : Cursorindexoutofboundsexception

Asked

Viewed 333 times

0

public boolean insertData(int id_pedido, int id_item, String nome, int quantidade) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1_ITEM_ORDER, id_pedido);
    contentValues.put(COL_2_ITEM_ORDER, id_item);
    contentValues.put(COL_3_ITEM_ORDER, nome);
    contentValues.put(COL_4_ITEM_ORDER, quantidade);
    String sql = "SELECT preco FROM " + TABLE_PRECO + " WHERE id_item = " + id_item;
    Cursor cursor = db.rawQuery(sql,null);
    cursor.moveToFirst();
    String preco_unitario = cursor.getString(cursor.getColumnIndex(COL_3_PRICES));
    cursor.close();
    double total = quantidade * Long.parseLong(preco_unitario);
    contentValues.put(COL_5_ITEM_ORDER, preco_unitario);
    contentValues.put(COL_6_ITEM_ORDER, total);
    long result = db.insert(TABLE_ITEM_PEDIDO, null, contentValues);
    if (result == -1) {
        return false;
    } else {
        return true;
    }

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

Can someone please assist me? I’ve made several changes and nothing.

  • 1

    Can you tell which line is the error?

1 answer

1


Fala Gabriel,

Probably this happens because your table is still empty, try to do so:

if(cursor.getCount() > 0){
  String preco_unitario = cursor.getString(cursor.getColumnIndex(COL_3_PRICES));
}

Hugs.

  • And Leandro! That’s what Victor said, my table was empty. Thanks for the force. It worked great now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.