개발/오류
[오류] indexoutofboundsexception, java.lang.NumberFormatException: For input string "string"
희세
2022. 7. 6. 14:09
java.lang.NumberFormatException: For input string "객체이름"
오류 내용
There was an unexpected error (type=Internal Server Error, status=500). For input string: "fruitType"
java.lang.NumberFormatException: For input string: "fruitType"
<c:if test="${fruit.fruitType eq fruitType.code}">selected</c:if>>${fruitType.name}</option>
parameterType을 Map으로 객체를 가져온 뒤, 그걸
Controller
에서 model에 속성 추가하여 넣고 화면에 뿌리는 작업이었는데 해당 오류가 뜸. 해당 오류의 근원지는 JSP
의 저 부분이었는데, model 속성 fruit에 든 fruitType과, model 속성 fruitTypeList에 든 code가 같은 값을 select option으로 만들면 된다. 하지만 저 비교식에서 왜인지 String type을 Number Type으로 형변환 시키려고 해서 오류 발생.model.addAttribute("fruit", fruitService.getfruitList(map));
Debug로 확인해 보니 Controller
의 해당 부분에서 indexoutofboundsexception
예외가 발생하고 있었다.
원인
indexoutofboundsexception 예외는 List형 객체의 index 범위를 벗어나는 경우나 선언되지 않는 요소를 get하는 경우 발생한다고 한다. Debug로 살펴보면 getFruitList의 parameter map이 null, size 0으로 값이 들어오지 않았는데(service와 mapper에서 왜인지 호출이 되다가 만다.) get을 해서 해당 오류가 발생된 것.
해결
if (map != null && map.size() != 0){
model.addAttribute("fruit", fruitService.getFruiteList(map));
}
if문으로 size를 판별하거나, ObjectUtils.isNotEmpty(map)을 통해 null과 empty를 validation하여 해결이 가능하다.