0

我有一个代码:

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

如果 testCustomer 为空,我如何签入@ResourceMapping?因为现在我NullPointer Exeption 的代码有什么问题?

4

1 回答 1

1

在 java 中,null与“为空”不同。在 a 上调​​用任何方法null都将导致 a NullPointerExceptionnull您必须在调用之前显式测试 for isEmpty()

将您的代码更改为:

if (testCustomer.getCustomer() == null || testCustomer.getCustomer().isEmpty())
于 2012-11-14T13:58:01.210 回答