直接上代碼如下:
@Controller
@RequestMapping("/views/information")
public class PubContentController extends BaseController{
@Autowired
private ContentCategoryService contentCategoryService;
/**
* 新增資訊
*
* @param pubContent
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/pubContent_add", method = RequestMethod.POST)
@AuthAnnotation(authStr = "func_news_add")
@LogAnnotation(module = "資訊管理", act = "新增")
@ResponseBody
public Object add(@ModelAttribute("pubContent") PubContent pubContent, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String currUserId = currUserId(request);
String title = pubContent.getTitle().trim();
if (StringHelper.isEmpty(title, true))
throw new ParameterException(MsgConstant.EXCEPTION_NAME_NULL);
//根據分類ID獲取資訊分類,再判斷是一級或二級分類
String categoryId = pubContent.getSubid();
Map<String,Object> params = Maps.newConcurrentMap();
if (!StringHelper.isEmpty(categoryId, true)){
params.put("id", categoryId);
}else{
throw new ParameterException(MsgConstant.EXCEPTION_CATEGORYID_NULL);
}
ContentCategory category = contentCategoryService.get("get", params);
String id = category.getId();
//父id
String pid = category.getPid();
if(StringHelper.isEmpty(pid, true) || pid.equals("0") ){
//沒有父id,是一級分類
//設置一級分類id
pubContent.setCid(id);
//設置二級分類id
pubContent.setSubid("");
}else{
//有父id,是二級分類
//設置一級分類id
pubContent.setCid(pid);
//設置二級分類id
pubContent.setSubid(id);
}
pubContent.setCreateBy(currUserId);
pubContent.setUpdateBy(currUserId);
int addid= pubContentService.insert(pubContent);
if (addid <= 0) {
return new Result(CodeConstant.RETCODE_500, null, null, MsgConstant.ERROR);
}
//更新新上傳的附件
uploadPubAttachment(request, response, pubContent.getId()+"");
CheckLog checkLog=new CheckLog();
checkLog.setPubid(addid);
checkLog.setCreateBy(currUserId);
checkLog.setNote("");
checkLog.setStatus(0);
checkLog.setUserName(currUser(request).getUserName());
checkLogService.insert(checkLog);
return new Result(CodeConstant.RETCODE_200, null, null, MsgConstant.SUCCESS);
}
/**
* 獲取
*
* @param id
* @return category
*/
@RequestMapping(value = "/get_Category", method = RequestMethod.POST)
@ResponseBody
private ContentCategory get_Category(@RequestParam(value = "id", required = true) String id,
HttpServletRequest request,
HttpServletResponse response, Model model)throws Exception {
Map<String,Object> params = Maps.newConcurrentMap();
if (!StringHelper.isEmpty(id, true)){
params.put("id", id);
}
ContentCategory category = contentCategoryService.get("get", params);
return category;
}
}
帶下划線的service是同一個service,綠色代碼處的是沒有問題,在紅色代碼處出現java.lang.NullPointerException異常問題,
后面查看對象,id是有值的, params的Map也是有值的,最后發現 contentCategoryService 對象是空的,找了好久都沒有發現
是什么問題導致了contentCategoryService 是空的,而調用另一個方法的contentCategoryService 是有實體對象的沒有任何問題,
最后請教大神指點,經過多次的測試發現原來是 訪問權限的問題,后一個contentCategoryService 為空的方法是訪問權限修飾符是private,
不為空的方法是訪問權限修飾符是public,所以導致了contentCategoryService為空,一直空指針異常。(自己排除錯誤的時候一直沒有
注意到訪問權限修飾符)修改了修飾符就解決了問題。
/**
* 獲取
*
* @param id
* @return category
*/
@RequestMapping(value = "/get_Category", method = RequestMethod.POST)
@ResponseBody
public ContentCategory get_Category(@RequestParam(value = "id", required = true) String id,
HttpServletRequest request,
HttpServletResponse response, Model model)throws Exception {
Map<String,Object> params = Maps.newConcurrentMap();
if (!StringHelper.isEmpty(id, true)){
params.put("id", id);
}
ContentCategory category = contentCategoryService.get("get", params);
return category;
}
復制粘貼,排除錯誤的時候記得要檢查權限修飾的