简体   繁体   中英

Getting Error while setting value in springboot?

This is what I have as a list, now I want only particular element to be shown in response rather then the entire, so I created a Response class and I am setting the values in it but the problem is when I am trying to set the value of data which is inside another list I am getting error as below, any sort of help is appreciated..

In this case this line is causing the issue:-

response.setData(item.get(i).getProd().get(i).getDescription())

Response:

"status": "Failed",
"message": "Error : Index 0 out of bounds for length 0",
    

Code:

public List < Response > getcollectionfromapi(List < String > itemids) throws Exception {
    List < Item > itemCollection = itemRepository.findById(itemids);
    Predicate < Item > nullcase = item - > item.getProduct().getName() == null;
    itemCollection.removeIf(nullcase);
    ArrayList < Response > arrayList = new ArrayList < > ();
    Response response = new Response();
    for (int i = 0; i < itemCollection.size(); i++) {
        response.set_id(itemCollection.get(i).get_id());
        response.setName(itemCollection.get(i).getName());
        response.setData(item.get(i).getProd().get(i).getDescription())
        arrayList.add(response);
    }
    return arrayList;
}

As the error states, you are out of the bounds of the array.

The array has no items (0) and you are trying to get the first item in this array, which is empty.

Try debugging the code to see the problem in detail.

My suggestion will be that the problem can be at the call of .getProd().get(i) where the first method returns an empty collection of elements.

As per your error: Index 0 out of bounds for length 0 you were trying to get item at position 0 where there are no items (length is 0).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM