2016-11-17 49 views
-1

我在Restful WebService中有一个方法,它根据ID打印给定记录的所有信息。打印方法中的NullPointerException

当我测试这个方法时,我得到一个NullPointerException。

@SuppressWarnings("null") 
@RequestMapping("print") 
@ResponseBody 
public ResponseEntity<Map<String, Object>> print(@RequestParam("id") Long id) { 
    Map<String, Object> mapPrint=null; 
    ResourceManage resourceManage = resourceManageService.findByIdwithowner(id); 
    Map<String, Object> params = new HashMap<String, Object>(); 
    if (resourceManage.getDocumentOwner() != null && resourceManage.getDocumentOwner().getId() != null) 
     params.put("documentOwner", resourceManage.getDocumentOwner().getId()); 
    else 
     params.put("documentOwner", null); 
    params.put("resId", id); 
    mapPrint.put("format", "pdf"); 
    mapPrint.put("reportQuery", resourceManageService.generateReportQuery(params)); 
    return new ResponseEntity<Map<String, Object>>(mapPrint, HttpStatus.OK); 
} 
+1

在这行你得到的NPE? – Christian

+0

你也应该提供一个这种方法的示例调用,它会导致NPE。 –

+1

可能重复[什么是NullPointerException,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – Jens

回答

3

由于您未实例化mapPrint,您将获得NPE。更改

Map<String, Object> mapPrint=null; 

Map<String, Object> mapPrint = new HashMap<>(); 
+0

谢谢。问题已经解决了。 –