为了账号安全,请及时绑定邮箱和手机立即绑定

尝试更改 for 循环中的值时出现错误

尝试更改 for 循环中的值时出现错误

慕斯709654 2023-07-28 09:38:46
我正在 Spring Boot 中开发一个应用程序。但我陷入了 Java 循环。为什么会犯这样的错误。即使我没有对 ArrayList 进行任何设置,也会发生此错误。java.lang.IndexOutOfBoundsException:索引:0,大小:0这是我的代码:    @RequestMapping("/update")    public String Update(@RequestParam(value = "this") int updateId,Model model,String newName,String newSurname,String newCountry) {        model.addAttribute("id",updateId);        model.addAttribute("name",personList.get(updateId).getName());        model.addAttribute("surname",personList.get(updateId).getSurname());        model.addAttribute("country",personList.get(updateId).getCountry());        for (Person person : personList) {            System.out.println(personList.size());            if (person.getId()==updateId) {                if (null!=newName) {                    person.setName(newName);                    updateControl+=1;                    System.out.println(personList.size());                }                if (null!=newSurname) {                    updateControl+=1;                    person.setSurname(newSurname);                }                if (null!=newCountry) {                    person.setCountry(newCountry);                    updateControl+=1;                }            }        }        if (updateControl==0) {            return "update";        }        else {            return "redirect:/";        }    }这是我的错误:There was an unexpected error (type=Internal Server Error, status=500).Index: 0, Size: 0java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
查看完整描述

3 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

如果你的personList为空,那么你就不能调用personList.get()它。您应该检查索引是否updateId小于personList大小。


@RequestMapping("/update")

public String Update(@RequestParam(value = "this") int updateId,Model model,String newName,String newSurname,String newCountry) {

    model.addAttribute("id",updateId);

    if (updateId < personList.size()) {

        model.addAttribute("name",personList.get(updateId).getName());

        model.addAttribute("surname",personList.get(updateId).getSurname());

        model.addAttribute("country",personList.get(updateId).getCountry());

    // ...

}

我还经常喜欢做的是使用保护子句:


@RequestMapping("/update")

public String Update(@RequestParam(value = "this") int updateId,Model model,String newName,String newSurname,String newCountry) {

    model.addAttribute("id",updateId);

    if (updateId >= personList.size()) {

        throw new EntityNotFoundException();

    }

    // ...

updateId如果您确定带有索引的元素肯定应该在那里,那么您可能也没有正确初始化或加载 personList 。


查看完整回答
反对 回复 2023-07-28
?
莫回无

TA贡献1865条经验 获得超7个赞

错误提示,列表中没有数据。但是您尝试从空列表中获取数据。

model.addAttribute("name",personList.get(updateId).getName());
model.addAttribute("surname",personList.get(updateId).getSurname());
model.addAttribute("country",personList.get(updateId).getCountry());

请检查您的 personList.


查看完整回答
反对 回复 2023-07-28
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

错误消息清楚地显示,您正在访问Index: 0列表中的元素 Size: 0。您可以在开始时添加 null 检查以避免这种情况,


if (null != personList && ! personList.isEmpty()) {

    //rest of code

}


查看完整回答
反对 回复 2023-07-28
  • 3 回答
  • 0 关注
  • 127 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信