gpt4 book ai didi

java - ModelMap 是空的吗?

转载 作者:行者123 更新时间:2023-12-01 15:02:55 25 4
gpt4 key购买 nike

我有一个代码:

public class testCustomer {

private ModelMap customer;
}

以及上述类的 getter/setter。

我有两种方法:

@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;
}

如果 testCustomer 为空,如何检查 @ResourceMapping ?因为现在我有 NullPointer Exception我的代码有什么问题?

最佳答案

在java中,null与“为空”不同。对 null 调用任何方法都会导致 NullPointerException。在调用 isEmpty() 之前,您必须显式测试 null

将代码更改为:

if (testCustomer.getCustomer() == null || testCustomer.getCustomer().isEmpty())

关于java - ModelMap 是空的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13380155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com