[JAVA, Spring] NullPointerException ํด๊ฒฐ๊ณผ์
ํน์ ๋ถ๋ถ์์ ์์ (PUT) ์์ฒญ์ ํ ์ 500 ์๋ฌ๊ฐ ๋ฌ๋ค๋ ์ด์๊ฐ ์์๋ค.
์ด์๋ฅผ ์ฌํํ๊ณ ๋ก๊ทธ๋ฅผ ํ์ธํ๋ WARN NullPointerException ~~ ์ด๋ ๊ฒ ๋ ์์๋ค.
1. ๋ฐ์ํ๋ ๋ถ๋ถ์ try catch๋ก ๋ฌถ์ด์ NullPointerException ์ ๋ก๊ทธ๋ฅผ ๋์์ฃผ์๋ค.

๋ก๊ทธ ๋ฉ์์ง๋ ์๋์ ๊ฐ์๋ค.
20231114 10:45:48.808 [http-nio-8090-exec-9] ERROR c.i.l.a.f.s.FileComponentUrlServiceImpl - NullPointerException caught:
java.lang.NullPointerException: null
at com.api.filecomponenturl.service.FileComponentUrlServiceImpl.updateUrl(FileComponentUrlServiceImpl.java:436)
NullPointerException์ด ๋ฐ์ํ๊ณ ,
updateUrl ๋ฉ์๋, FileComponentUrlServiceImpl.java:436๋ผ์ธ์ด๋ผ๊ณ ์๋ ค์ฃผ๊ณ ์๋ค.
436๋ผ์ธ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์๋ค.
// json ํ์ ๊ฒ์ฌ
if (!urlUpdateReq.getExtInfo().isEmpty() && !UrlUtil.checkValidJson(urlUpdateReq.getExtInfo())) {
throw new ApiException("error.fileComponent.invalidExtInfoFormat", HttpStatus.BAD_REQUEST);
}
๋๋ฒ๊น
์ ์ํด ํด๋น ๋ผ์ธ์์ ์ฐธ์กฐ๋๋ ๋ชจ๋ ๊ฐ์ฒด๊ฐ null์ธ์ง ์๋์ง ํ์ธํด์ผํ๋ค.
ํด๋น ๋ผ์ธ์์ null์ด ๋ ์ ์๋ ๊ฐ์ฒด๋ urlUpdateReq์ urlUpdateReq.getExtInfo() ์ด๋ฉฐ, UrlUtil.checkValidJson ๋ฉ์๋ ๋ด๋ถ์์๋ null ์ฐธ์กฐ๊ฐ ๋ฐ์ํ ์ ์์ด ๊ฒํ ํด์ผํ๋ค.
- urlUpdateReq์ null ์ฒดํฌ: ๋จผ์ urlUpdateReq ๊ฐ์ฒด๊ฐ null์ธ์ง ํ์ธ. ์ด ๊ฐ์ฒด๊ฐ null์ด๋ฉด, ๋ฉ์๋ ๋ด๋ถ์์ urlUpdateReq.getExtInfo()๋ฅผ ํธ์ถํ ๋ NullPointerException์ด ๋ฐ์ํ๋ค.
- getExtInfo()์ ๋ฆฌํด ๊ฐ ์ฒดํฌ: urlUpdateReq๊ฐ null์ด ์๋๋ผ๋ฉด, getExtInfo() ๋ฉ์๋์ ๋ฐํ ๊ฐ์ด null์ธ์ง ํ์ธ. isEmpty() ๋ฉ์๋๋ null์ ๋ํด ํธ์ถํ ์ ์์ผ๋ฏ๋ก, ์ด ๋ถ๋ถ์์๋ NullPointerException์ด ๋ฐ์ํ ์ ์๋ค.
- UrlUtil.checkValidJson ๋ฉ์๋ ๊ฒ์ฌ: ์ด ๋ฉ์๋ ๋ด๋ถ์์ null์ ์ ์ ํ ์ฒ๋ฆฌํ๊ณ ์๋์ง ํ์ธ. ์ ๋ ฅ ๊ฐ์ผ๋ก null์ด ๋ค์ด๊ฐ ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ก์ง์ด ์๋์ง ๊ฒํ ํด์ผ ํ๋ค.
์ด๋ฅผ ํ์ธํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋์ ๋ก๊ทธ๋ฅผ ์ถ๊ฐํ๋ค.
if (urlUpdateReq == null) {
log.error("urlUpdateReq is null");
throw new IllegalArgumentException("urlUpdateReq cannot be null");
}
String extInfo = urlUpdateReq.getExtInfo();
if (extInfo == null) {
log.error("extInfo in urlUpdateReq is null");
throw new IllegalArgumentException("extInfo cannot be null");
}
if (!extInfo.isEmpty() && !UrlUtil.checkValidJson(extInfo)) {
log.error("Invalid JSON format in extInfo: " + extInfo);
throw new ApiException("error.fileComponent.invalidExtInfoFormat", HttpStatus.BAD_REQUEST);
}
์ด๋ ๊ฒ ์ฝ๋๋ฅผ ์์ ํ๊ณ ์ํฉ์ ์ฌํํด๋ณด๋
20231114 11:01:47.600 [http-nio-8090-exec-3] ERROR c.i.l.a.f.s.FileComponentUrlServiceImpl - extInfo in urlUpdateReq is null
20231114 11:01:47.613 [http-nio-8090-exec-3] WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved [java.lang.IllegalArgumentException: extInfo cannot be null]
extInfo in urlUpdateReq is null ์กฐ๊ฑด๋ฌธ์ด ์ถ๋ ฅ๋์๋ค.
์๋์ ๊ฐ์ด ํด๊ฒฐํ ์ ์์๋ค.
// json ํ์ ๊ฒ์ฌ
String extInfo = urlUpdateReq.getExtInfo();
if (extInfo != null && !extInfo.isEmpty() && !UrlUtil.checkValidJson(extInfo)) {
throw new ApiException("error.fileComponent.invalidExtInfoFormat", HttpStatus.BAD_REQUEST);
}
!= null ๊ณผ isEmpty()์ ์ฐจ์ด๋ ์๋์ ๊ธ์์ ๋ค๋ฃจ์๋ค.
https://yesolz.tistory.com/entry/JAVA-null%EA%B3%BC-isEmpty%EC%9D%98-%EC%B0%A8%EC%9D%B4
[JAVA] null๊ณผ isEmpty์ ์ฐจ์ด
info != null: ์ด ์กฐ๊ฑด์ info ๋ณ์๊ฐ null์ธ์ง ์๋์ง๋ฅผ ๊ฒ์ฌ **null์ ๋ณ์๊ฐ ์๋ฌด๋ฐ ๊ฐ์ฒด๋ ์ฐธ์กฐํ์ง ์๊ณ ์์์ ์๋ฏธํจ** ๊ฐ์ฒด๊ฐ null์ธ ๊ฒฝ์ฐ, ํด๋น ๊ฐ์ฒด์ ์ด๋ ํ ๋ฉ์๋๋ ํธ์ถํ ์ ์์ผ๋ฉฐ, ์ด๋ฅผ
yesolz.tistory.com