直接上代码如下:
@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;
}
复制粘贴,排除错误的时候记得要检查权限修饰的