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 。

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
.

TA貢獻1943條經驗 獲得超7個贊
錯誤消息清楚地顯示,您正在訪問Index: 0列表中的元素 Size: 0。您可以在開始時添加 null 檢查以避免這種情況,
if (null != personList && ! personList.isEmpty()) {
//rest of code
}
添加回答
舉報