본문 바로가기

NullPointerException3

[Java] Optional은 왜 사용할까? (Feat. NullPointerException) 자바 프로그래밍을 하면서 NPE를 겪어보지 않은 사람은 없을것이다. null 처리를 하기 위해 우리는 많은 노력을 한다. 모든 객체를 null 체크, null 일 경우에 대한 처리 if(person != null){ //null이 아닐 때만 코드 수행 } ################# if(person == null){ throw new PersonNotExistException(); } 이 경우 객체의 개수 만큼 if절이 늘어나게 되어 가독성에 좋지 않다. Optional (java.util.Optional) 자바8 에서는 Optional이라는 새로운 클래스를 제공한다. 예를 들어, 위의 person 객체가 있다고 가정했을 때 Optional 처럼 객체를 감싼다. 값이 있을 경우 객체를 감싸지만, 없.. 2021. 5. 14.
JPA repository.save is Null (NullPointerException) - feat. @RunWith & @SpringBootTest & @DataJpaTest JPA로 save 를 작성해보던 도중 @Autowired private UserRepository userRepository; @Test public void create(){ System.out.println(userRepository); User user = new User(); user.setAccount("TestUser01"); user.setEmail("TestUser01@gmail.com"); user.setPhoneNumber("010-1111-1111"); user.setCreatedAt(LocalDateTime.now()); user.setCreatedBy("admin"); System.out.println(user); User newUser = userRepository.save(us.. 2019. 11. 13.
스택 트레이스 읽는 방법(feat. NullPointerException) 스택 트레이스르 정확히 읽을 줄 알아야 빠른 디버깅이 가능하고, 이는 에러 해결하는 시간을 줄여주는데 직결하기 때문에 알아두는게 좋을 것 같다. java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.mylibrary.ap.xul.build.. 2019. 8. 5.