Debug Log

테스트코드)Cannot invoke "String.isBlank()" because the return value of "com.project.sparta.recommendCourse.dto.RecommendRequestDto.getTitle()" is null

밍미a 2023. 3. 10. 12:06
728x90

에러체크를 하다가 

Cannot invoke "String.isBlank()" because the return value of "com.project.sparta.recommendCourse.dto.RecommendRequestDto.getTitle()" is null

에러를 만났다.

수정되는 값에 널이 들어가면 어떻게 처리되는지 확인해주고 싶었는데

내가 예상한대로 커스텀이셉션이 안뜨고 에러가 발생했다

왜지..?

일단 에러코드를 읽어보니까

Cannot invoke "String.isBlank()" because the return value of "com.project.sparta.recommendCourse.dto.RecommendRequestDto.getTitle()" is null

 

라고 한다.

번역하면

반환 값이 "com.project.sparta.remoteCourse.dto"이므로 "String.isBlank()"를 호출할 수 없습니다.RecommendRequestDto.getTitle()"이(가) null입니다

 

에러코드를 검증하는 로직으로 가보니

// 에러1: Title, Season, Contents, Region 중 ""가 포함된 경우
if (requestDto.getTitle().isBlank() || requestDto.getSeason().isBlank()
        || requestDto.getContents().isBlank() || requestDto.getRegion().isBlank()) {
    throw new CustomException(INVALID_CONTENT);
}
// 에러2: 숫자가 null인 경우
if (requestDto.getScore() == 0 || requestDto.getAltitude() == 0) {
    throw new CustomException(INVALID_CONTENT);
}
// 에러3: 컨텐츠가 null인 경우
if (requestDto.getTitle() == null || requestDto.getSeason() == null ||
        requestDto.getContents() == null || requestDto.getRegion() == null) {
    throw new CustomException(INVALID_CONTENT);
}

이렇게 되어있었다.

아.. null이라서 첫번째 if문을 안타는구나.. 그런데 elif 처리를 해준게 아니라서 저 세개의 if문은 무조건 다 거쳐야 하는 거니까 null을 먼저 검증하고 blank를 검증을 해야겠네..!

순서를 한번 바꿔보자


// 에러1: 숫자가 null인 경우
if (requestDto.getScore() == 0 || requestDto.getAltitude() == 0) {
    throw new CustomException(INVALID_CONTENT);
}
// 에러2: 컨텐츠가 null인 경우
if (requestDto.getTitle() == null || requestDto.getSeason() == null ||
        requestDto.getContents() == null || requestDto.getRegion() == null) {
    throw new CustomException(INVALID_CONTENT);
}

// 에러3: Title, Season, Contents, Region 중 ""가 포함된 경우
if (requestDto.getTitle().isBlank() || requestDto.getSeason().isBlank()
        || requestDto.getContents().isBlank() || requestDto.getRegion().isBlank()) {
    throw new CustomException(INVALID_CONTENT);
}

 

오,..! 잘 된다.>!

널처리를 제일 먼저 해야하는구나.. 하나 배웠다