Java
[Java] HashSet 합집합 add(), addAll() 시 UnsupportedOperationException 날 경우
HashSet을 합치려고 시도하다가 UnsupportedOperationException 를 마주했다. 해결 후 기록으로 남겨두면 좋을 것 같아서 여기에 남긴다. 예를 들어 코드가 이런식이다. 코드: Map hashMap1 = new HashMap(); Map hashMap2 = new HashMap(); Set mySet1 = new HashSet(); Set mySet2 = new HashSet(); hashMap1 = (맵1 불러오기 코드) hashMap2 = (맵2 불러오기 코드) mySet1 = hashMap1.keySet() mySet2 = hashMap2.keySet() mySet1.addAll(mySet2); // 오류 오류메시지: java.lang.UnsupportedOperationEx..
[Java] 자바 Null 체크, 빈 값("") 체크
데이터 값을 받아올 때 가장 쉽게 마주할 수 있는 오류는 NullPointerException(NPE) 이다 . 보통 객체 값이 null이거나 초기화가 안되어있는 인스턴스를 사용하면서 NPE를 마주하게 된다. - null 객체에서 method를 호출하는 경우 - null 객체의 필드에 접근하거나 값을 변경하는 경우 - null 의 길이를 배열처럼 취하는 경우 - null 을 throw 하는 경우 - null 을 통해 동기화 할 경우 public class Sample { public static void main(String[] args) { Go go = null; go.toString(); // NullPointerException go.equals(""); // NullPointerException..