5.30 부동산 API NullPointException 원인과 해결, DB설계

김용희·2023년 7월 18일

절대 commit을 잊지 마.. (직접 데이터를 DB에 넣은 경우엔 commit 꼭 하자)


부동산 데이터 중에 '건축년도' 없는 놈이 있어서 얘 때문에 null exception

해결완료 (nullpointException 해결완료)

코드를 보면 알겠지만
단순히 if(nodeList == null) 하면 null 체크가 안됨.
왜냐면 nodeList는 객체라서 번지(주소) 값을 갖고 있거든

  • 따라서 null 체크를 하기 위해선
  • (1) nodeList.item(0) == null
    또는
  • (2) nodeList.getLength( ) == 0
    이렇게 체크를 해줘야 했음!!
private static String getTagValue(String tag, Element eElement) {
		String tagValue = null;
		NodeList nodeList = eElement.getElementsByTagName(tag); // tag == 건축년도
		NodeList childNodeList = null;
		Node nValue = null;
		
		// ★★★★★ 해당 tag가 없을 때 값이 어떻게 들어오는지 봤더니
		if(tag.equals("건축년도")) {
			System.out.println(nodeList); // (1) 그냥 nList는 객체이고 (com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@2e222612)
			System.out.println(nodeList.item(0)); // (2) nList.item(0)이 null값으로 찍히며 ([건축년도: null] => null (우측의 값이 건축년도 태그 없는 경우))
			System.out.println(nodeList.getLength()); // (3) nList.getLength()의 경우엔 0이 찍힘 (1 => 0)
		}
		
		// ★★★★★ 따라서 null 체크를 하는 방법으로는 item(0) 또는 getLength()를 쓰면 되겠다!
		if(nodeList.getLength() == 0) { // <=> nList.item(0) == null 로도 치환가능
			tagValue = "";
		}else {
			childNodeList = nodeList.item(0).getChildNodes();
			nValue = (Node) childNodeList.item(0);
			
			if (nValue == null) {
				tagValue = ""; // 원래 null 리턴하도록 돼있었는데 ""로 바꿔줌 (나중에 JPA가 null 인식(그건 스프링프레임워크인가..?))
			}
			
			tagValue = nValue.getNodeValue().trim();
		}
		
		return tagValue; 
	}

@Test 결과

부동산 district 테이블

코드를 좀 잘못 짰다;;; 다시 짜야겠다 코드


임시 table diagram

profile
쓰용

1개의 댓글

comment-user-thumbnail
2023년 7월 18일

정말 좋은 글 감사합니다!

답글 달기