I'm learning about Optional<>
used in implementation with JPA and Hibernate. And I see a lot of developers are using these 2 methods isEmpty() or isPresent() like here:
private void checkCustomer(UUID customerId) {
Optional<Customer> customer = customerRepository.findCustomer(customerId);
if (customer.isEmpty()) {
log.warn("Could not find customer with customer id: {}", customerId);
throw new CustomerException(...);
}
}
I want to know if this isEmpty() method checks if the customer is null or empty. For example if there is no customer with this id in the database, then the variable customer will be null or empty? I ask that because if the isEmpty() method checks if the customer is empty, but the customer is null, in this case can I get a NullPointerException at this line customer.isEmpty()
? Or if I use customer.isPresent()
can I get a NullPointerException?