Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试更改for循环中的值时出错_Java_Spring Boot - Fatal编程技术网

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

Java 尝试更改for循环中的值时出错,java,spring-boot,Java,Spring Boot,我正在用Spring Boot开发一个应用程序。 但我陷入了Java循环。为什么会犯这样的错误呢。 即使我没有对ArrayList进行任何设置,也会发生此错误。 java.lang.IndexOutOfBoundsException:索引:0,大小:0 这是我的代码: @RequestMapping("/update") public String Update(@RequestParam(value = "this") int updateId,Model model,Stri

我正在用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

如果您的
personList
为空,则无法对其调用
personList.get()
。 您应该检查索引
updateId
是否小于
个人列表的大小

@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());
    // ...
}

如果您确定索引为
updateId
的元素肯定存在,则可能您也没有正确初始化或加载personList。

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

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

请检查您的
个人列表

错误消息,该消息清楚地显示,您正在访问
大小:0
列表中的
索引:0
元素。 您可以在开头添加空检查以避免出现这种情况

if (null != personList && ! personList.isEmpty()) {
    //rest of code
}

你能把填写和实例化ArrayList的部分添加到你的帖子中吗?personList是从哪里来的?这个错误意味着你试图获取空列表的第一个元素
if (null != personList && ! personList.isEmpty()) {
    //rest of code
}