NullPointerException
: 가리키는 것이 없다.
: 뜻. 메모리에 없다.
heap 메모리에 저장되어 있는 것이 없다는 뜻이므로 객체를 생성해줘야 한다.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.add(Object)" because "mc.memberList" is null at main.AddressMain.main(AddressMain.java:21)
객체를 생성하는 키워드 = new
- ArrayList를 사용할 때 오류가 잘 나기 때문에 신경 쓰자.
예)
ArrayList<Employer> employerArrayList = new ArrayList<>();
heap 메모리에 저장하는 멤버 변수
- 코드 작성 중 로컬변수로 사용하던 변수를 멤버변수로 사용해야 하는 경우들이 있다.
그럴 경우 신경 써서 멤버 변수로 만들어서 사용하자.