Preloader image
DDD

자바

올바른 NPE(NullPointException) Check - StringUtils / @Notnull

작성자 관리자 (admin)
조회수 882
입력일 2021-11-01 11:56:42

NULL인 객체

객체의 method를 호출하는 경우
객체의 접근하는 경우
객체의 length를 구하려는 경우
객체의 값을 인덱스로 접근하는 경우
Controller에 존재하는 변수가 View에서 NULL인 경우

1. org.apache.commons.lang.StringUtils 함수 사용

package com.erp.common.function;
import org.apache.commons.lang.StringUtils;

public class NullPointEx
{
    public static void main(String[] args)
    { 
        String a = null;
        String b = "헐~";
        String c = "";

        System.out.println( "●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●" );
        System.out.println( "a - " +  StringUtils.isEmpty (       null )); // true 
        System.out.println( "a - " +  StringUtils.equals  (    a, null )); // true 
        System.out.println( "a - " +  StringUtils.equals  ( null,    a )); // true 
        System.out.println( "●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●" );
        System.out.println( "b - " +  StringUtils.isEmpty (       null )); // true 
        System.out.println( "b - " +  StringUtils.equals  (    b, null )); // false
        System.out.println( "b - " +  StringUtils.equals  ( null,    b )); // false
        System.out.println( "●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●" );
        System.out.println( "c - " +  StringUtils.isEmpty (       null )); // true 
        System.out.println( "c - " +  StringUtils.equals  (    c, null )); // false
        System.out.println( "c - " +  StringUtils.equals  ( null,    c )); // false
        System.out.println( "●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●" );
    }

}

2. Spring VO @Nonnull

   @Nonnull private int    start   = 0  ; // Oracle 시작
   @Nonnull private int    end    = 0  ; // Oracle 끝
   @Nonnull private String Name   = 0  ; // 이름

 

^