NullPointerException : null값을 갖는 참조 변수로 객체 접근 연산자인 도트(.)를 사용했을 때 발생
→ 객체가 없는 상태에서 객체를 이용하려 했으니 예외가 발생
NullPointerException이 발생하는 경우
package sec02_runtime_exception;
public class NullPointerExceptionExample {
public static void main(String[] args) {
String data = null;
System.out.println(data.length()); //NullPointerException 발생
}
}
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "data" is null
at sec02_runtime_exception.NullPointerExceptionExample.main(NullPointerExceptionExample.java:6)
'Java > 10. 예외 처리 (Exception)' 카테고리의 다른 글
Day 14 : 예외 처리 코드 - try-catch-finally 블록 (0) | 2021.11.05 |
---|---|
Day 14 : ClassCastException (0) | 2021.11.05 |
Day 14 : NumberFormatException (0) | 2021.11.05 |
Day 14 : ArrayIndexOutOfBoundsException (0) | 2021.11.05 |
Day 14 : 예외 (Exception) (0) | 2021.11.05 |