Check for an empty array before starting the loop.
If you loop over an array without checking if it's empty, you might encounter errors. Before starting a loop, verify the array isn't empty to avoid processing issues. For an empty cart: if (cartItems.length > 0) { for (int i = 0; i < cartItems.length; i++) { total += cartItems[i].price; } }.