Skip to content
Advertisement

IndexOutOfBoundException when i pass arraylist to DBHelper to update database (Android SQlite)

I am new at android and trying to make managing members app with database. what i want to do here is that when user buys any drink it should change drinks name to “bought” in database, but when i pass Arraylist to db class it shows that my Arraylist is empty.

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:437)
        at com.shashank.managemembers.DB_Controller.update_available(DB_Controller.java:88)
        at com.shashank.managemembers.TempActivity$2.onClick(TempActivity.java:90)

userInputActivity

here user selects drink he/she wants and when he presses done button it should change database with drinks name. And I’m checking it with Toast message that ArrayList is not empty. Toast message always appears with user’s choice but db class throws error.

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TempActivity.this, selectedInListView.get(0), Toast.LENGTH_SHORT).show();
                dbController.update_available(code, selectedInListView);
                onBackPressed();
            }
        });

here selectedInListView has many items when i am passing it to dbController.update_available.

dbController.java

here I have 9 columns in db starting from DRINK1 to DRINK9.

public void update_available(String memberCode, ArrayList<String> BoughtDrinks) {
    Cursor cursor = this.getReadableDatabase().rawQuery("SELECT * FROM MEMBERS WHERE MEMBERCODE = '" + memberCode + "'", null);
    int count = cursor.getColumnCount();
    while (cursor.moveToNext()) {
        for (int i = 5; i < count; i++) {
            if (!cursor.getString(i).contains(BoughtDrinks.get(0))) {
                this.getReadableDatabase().execSQL("UPDATE MEMBERS SET DRINK"+ i +"='Bought' WHERE DRINK" + i +"='" + BoughtDrinks.get(0) + "'" + "AND MEMBERCODE='" + memberCode + "'");
                if(BoughtDrinks.size() > 0 ){
                    BoughtDrinks.remove(0);
                }
            }
        }
    }
    cursor.close();
}

I am not understanding why it is throwing an error when I’m passing ArrayList with elements.

Advertisement

Answer

You got this error because ,if BoughtDrinks.size()>0 ,your code remove item at 0 then for loop get item at 0.So when you try to get item at 0 after you removed all the item ,you will get index IndexOutOfBoundsException.to avoid that just add if() condition.

 public void update_available(String memberCode, ArrayList<String> BoughtDrinks) {
        Cursor cursor = this.getReadableDatabase().rawQuery("SELECT * FROM MEMBERS WHERE MEMBERCODE = '" + memberCode + "'", null);
        int count = cursor.getColumnCount();
        while (cursor.moveToNext()) {
            for (int i = 5; i < count; i++) {
                if ( BoughtDrinks.size()>0 && !cursor.getString(i).contains(BoughtDrinks.get(0))) {
                    this.getReadableDatabase().execSQL("UPDATE MEMBERS SET DRINK"+ i +"='Bought' WHERE DRINK" + i +"='" + BoughtDrinks.get(0) + "'" + "AND MEMBERCODE='" + memberCode + "'");
                    if(BoughtDrinks.size() > 0 ){
                        BoughtDrinks.remove(0);
                    }
                }
            }
        }
        cursor.close();
    }
1 People found this is helpful
Advertisement