



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

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


정말 좋은 글 감사합니다!