2012-11-14 88 views
0

我有一個代碼:上面類ModelMap是否爲空?

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

我怎麼能檢查在@ResourceMapping如果testCustomer爲空?因爲我現在有NullPointer Exeption 我的代碼有什麼問題?

+0

那一行你得到的NPE? – Bohemian

+0

在線if(testCustomer.getCustomer()。isEmpty()) – faszynski

回答

1

在java中,null與「空着」不同。調用null上的任何方法將導致NullPointerException。在致電isEmpty()之前,您必須明確地測試null

你的代碼改成這樣: