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

我正在 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: 0

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0


慕斯709654
浏览 129回答 3
3回答

墨色风雨

如果你的personList为空,那么你就不能调用personList.get()它。您应该检查索引是否updateId小于personList大小。@RequestMapping("/update")public String Update(@RequestParam(value = "this") int updateId,Model model,String newName,String newSurname,String newCountry) {&nbsp; &nbsp; model.addAttribute("id",updateId);&nbsp; &nbsp; if (updateId < personList.size()) {&nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("name",personList.get(updateId).getName());&nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("surname",personList.get(updateId).getSurname());&nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("country",personList.get(updateId).getCountry());&nbsp; &nbsp; // ...}我还经常喜欢做的是使用保护子句:@RequestMapping("/update")public String Update(@RequestParam(value = "this") int updateId,Model model,String newName,String newSurname,String newCountry) {&nbsp; &nbsp; model.addAttribute("id",updateId);&nbsp; &nbsp; if (updateId >= personList.size()) {&nbsp; &nbsp; &nbsp; &nbsp; throw new EntityNotFoundException();&nbsp; &nbsp; }&nbsp; &nbsp; // ...updateId如果您确定带有索引的元素肯定应该在那里,那么您可能也没有正确初始化或加载 personList 。

莫回无

错误提示,列表中没有数据。但是您尝试从空列表中获取数据。model.addAttribute("name",personList.get(updateId).getName()); model.addAttribute("surname",personList.get(updateId).getSurname()); model.addAttribute("country",personList.get(updateId).getCountry());请检查您的&nbsp;personList.

杨__羊羊

错误消息清楚地显示,您正在访问Index: 0列表中的元素 Size: 0。您可以在开始时添加 null 检查以避免这种情况,if (null != personList && ! personList.isEmpty()) {&nbsp; &nbsp; //rest of code}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java