I Have a code:
public class testCustomer {
private ModelMap customer;
}
And getters/setters for above class.
And I have two methods:
@ActionMapping(params = "action=search")
public void searchAction(
ActionRequest request,
ActionResponse response) {
ModelMap modelMap = new ModelMap();
modelMap.put("rowsPerPage", 10);
modelMap.put("page", 1);
modelMap.put("total", 100);
testCustomer.setCustomer(modelMap);
}
@ResourceMapping(value="customer")
public ModelAndView listCustomer(
ResourceRequest req,
ResourceResponse res) {
ModelAndView mav = new ModelAndView();
MappingJacksonJsonView v = new MappingJacksonJsonView();
mav.setView(v);
if(testCustomer.getCustomer().isEmpty()){
log.debug("List Customer Resource: " + "NULL");
mav.addObject("data", null);
} else {
log.debug("List Customer Resource: " + testCustomer.getCustomer());
mav.addObject("dataListCustomer", testCustomer.getCustomer());
}
return mav;
}
How Can I check in @ResourceMapping if testCustomer is empty or not? Because now I have NullPointer Exeption
What is wrong in my code?
In java,
nullis different to “being empty”. Calling any method on anullwill result in aNullPointerException. You must explicitly test fornullbefore callingisEmpty().Change your code to this: