ModelMap是空的吗?

时间:2012-11-14 13:55:02

标签: java spring

我有一个代码:

public class testCustomer {

    private ModelMap customer;
    }

以上课程的吸气人/安装人员。

我有两种方法:

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

我如何检查@ResourceMapping testCustomer是否为空?因为我现在有NullPointer Exeption 我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

在java中,null与“空”不同。在null上调用任何方法都会产生NullPointerException。在调用null之前,您必须明确测试isEmpty()

将您的代码更改为:

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