亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試更改 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 關注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號