-1

Getting java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) [testng] at java.util.ArrayList.get(ArrayList.java:435) Below is the code where issue is found:

 public String getPaymentInstrumentId(Customer customer) {
   String paymentinstrumentidtest;
   List < String > paymentInstrumentIds = new ArrayList < String > ();
   for (int currentPaymetMethod = 0; currentPaymetMethod < customer.getPaymentMethods().size(); currentPaymetMethod++) {

     String currentPaymentInstrumentId = customer.getPaymentMethods().get(currentPaymetMethod)
       .getPaymentInstrumentId();
     paymentInstrumentIds.add(currentPaymentInstrumentId);
   }

   return paymentinstrumentidtest;
 }

Any suggestion would help ?

Deepi
  • 9

1 Answers1

0
  1. Might be a thread concurrency issue. If other thread modifies the List resulted by customer.getPaymentMethods() in the same time you want to access it there then this kind of error will occur since.

In this example .size() will return a value > 0. Another thread releases the list or deletes all the content. Now you want to access the first element with .get(Index: 0) of the list but the list is empty.

  1. Check really well your methods for side effects. Eg. be sure that .size() or .getPaymentMethods() doesn't also clears your list.
Stoica Mircea
  • 694
  • 9
  • 21