【问题标题】:Getting Error while setting value in springboot?在spring boot中设置值时出错?
【发布时间】:2023-04-05 16:35:01
【问题描述】:

这是我作为列表的内容,现在我只想在响应中显示特定元素而不是整个元素,因此我创建了一个 Response 类并在其中设置值但问题是当我尝试时要设置另一个列表中的数据值,我收到如下错误,感谢任何形式的帮助..

在这种情况下,这条线导致了问题:-

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

回复:

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

代码:

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;
}

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    由于错误状态,您超出了数组的范围。

    数组中没有项 (0),您正在尝试获取此数组中的第一项,它是空的。

    尝试调试代码以详细查看问题。

    我的建议是问题可能出在.getProd().get(i) 的调用上,其中第一个方法返回一个空的元素集合。

    根据您的错误:Index 0 out of bounds for length 0 您试图在没有项目(长度为 0)的位置 0 处获取项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 2018-04-24
      • 1970-01-01
      • 2016-08-15
      • 2017-05-12
      • 2017-05-24
      • 1970-01-01
      • 2019-01-01
      相关资源
      最近更新 更多