Request processing failed; nested exception is java.lang.NumberFormatException: Cannot parse null string
게시글에 업로드한 파일을 다운로드 받는 코드를 만들던 중 이 알람이 발생했다.
차근차근 확인해본 결과
mv.addObject("attFile",service.getDownloadFileInfo(
Integer.parseInt(params.get("attachSeq"))
));
이 코드에서 params.get("attachSeq") 의 값이 null인것을 발견했다.
<a href='<%=ctx%>/forum/download.do?attachSeq=${attFile.attachSeq}'>
fileName="${attFile.orgFileNm}" fileSize="${attFile.fileSize}"
</a>
read.jsp에서 파일 이름을 클릭하면 download.do로 attachSeq값을 전송해준다.
JSTL로 출력해서 확인해본 결과 attachSeq값은 잘 나옴. > 컨트롤러로 전송 또한 잘 됨
***문제점 발견
// 새로 추가한 코드
public BoardAttachDto getAttachInfo(int attachSeq) {
String sql = "SELECT * FROM board_attach ba "
+ "WHERE ba.attach_seq = ? ";
Object[] args = { attachSeq };
return queryForObject(sql, new BoardAttachRowMapper(), args);
}
//기존에 있던 코드
public BoardAttachDto getAttachInfo(int boardSeq,int boardTypeSeq) {
String sql = "SELECT * FROM board_attach ba "
+ "WHERE ba.board_seq = ? "
+ " AND ba.board_type_seq = ?";
Object[] args = {boardSeq,boardTypeSeq};
try {
return queryForObject(sql, new BoardAttachRowMapper(), args);
} catch (EmptyResultDataAccessException e) {
// 파일 첨부 정보가 없는 경우에도 null을 반환하도록 수정
return null;
}
}
오버로딩이란걸 까먹고 있었다.
기존에 있던 코드를 유지한 채로 강사님이 올려준 새로 추가한 코드를 적어줬어야 하는데
기존에 있던 코드를 삭제하니까 에러가 났던 것.
*오버로딩*
기존에 있던 코드의 역할 : boardSeq와 boardTypeSeq를 매개변수로 받아
DB에 해당 매개변수와 일치하는 파일이 있는지 없는지 검사
새로 추가한 코드의 역할 : attachSeq를 매개변수로 받아
DB에 해당 매개변수와 일치하는 파일이 있는지 없는지 검사
서비스에서 아래와 같이 따로 메서드를 만들어 각각 기능을 수행함
public BoardAttachDto getAttFile(int boardSeq, int boardTypeSeq) {
return boardAttachDao.getAttachInfo(boardSeq,boardTypeSeq);
}
public BoardAttachDto getDownloadFileInfo(int attachSeq) {
return boardAttachDao.getAttachInfo(attachSeq);
}
| 파일첨부시 파일이 안넘어왔던 이유가 뭘까? (1) | 2024.05.20 |
|---|---|
| DateFormat 작성 시 주의할 점 (1) | 2024.05.14 |
| EmptyResultDataAccessException 처리 (1) | 2024.05.13 |
| Dao새로 만들었을때 주의할점 (0) | 2024.05.11 |
| 게시글 작성 후 새로고침하면 계속 같은 게시글이 생성되는 에러 (0) | 2024.05.10 |