I use this code to get Merchant name by ID.
@GetMapping("pages")
public Page<WpfPaymentsDTO> pages(@RequestParam(value = "page") int page, @RequestParam(value = "size") int size) {
return wpfPaymentsService.findAll(page, size)
.map(g -> WpfPaymentsDTO.builder()
.id(g.getId())
.status(g.getStatus())
.merchant_id(getMerchantName(g.getMerchant_id()))
.build());
}
private String getMerchantName(int id) {
Optional<Merchants> obj = merchantService.findById(id);
return obj.get().getName();
}
But when name is not found I get java.lang.NullPointerException: null
at this line: .merchant_id(getMerchantName(g.getMerchant_id()))
Is there some way to set empty string and not to break the code when Object is not found?