Android SortedList java.lang.IndexOutOfBoundsException: Asked to get item at 0 but size is 0

40 views Asked by At
public Order gimmeThePendingOrderToDeliver(String _id, String my_phone) {
        for (int i = 0; i < orderSortedList.size(); i++) {
            Order o = orderSortedList.get(i);

            boolean a = o.isCanceled_by_user(), b = o.isCanceled_by_owner(), c = o.isDelivered();
            String d = o.getDelivery_guy();
            if (_id.equals(o.get_id()) && !a && !b && !c && d.equals(my_phone)) {
                Toast.makeText(context, "IM HERE", Toast.LENGTH_SHORT).show();
                return o;
            }
        }
        Toast.makeText(context, orderSortedList.get(0).get_id(), Toast.LENGTH_SHORT).show();
        return null;
    }

cant iterate Android SortedList returns error as descriped in the title ,, any help will be appreciated . THANKS in advance.

1

There are 1 answers

0
Vlad Guriev On

When orderSortedList is empty, it doesn't enter the for loop, and thus, it immediately reaches the bottommost Toast statement where orderSortedList.get(0) produces an IndexOutOfBoundsException since the list is empty.