500状态码
前言
前几天运行项目前端页面找不到js文件 报错406
一、出现500状态码空指针异常
服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理。一般来说,这个问题都会在服务器端的源代码出现错误时出现,不只是空指针,敲代码遇到最多的错误就是500
二、原因
代码如下(示例):
@RequestMapping("/demandprojecttype")
public String demandprojecttype(Integer prohectTypeId,HttpSession session,Model m){
//获取session设置的参数
String demandProjectName=(String) session.getAttribute("DemandProjectName");
//输出前端获取的参数
System.out.println("prohectTypeId:"+prohectTypeId);
//输出此参数
System.out.println("demandProjectName:"+demandProjectName);
//调用service方法查询数据库
List<Demand> demands=demandService.demandprojecttype(prohectTypeId,demandProjectName);
//Moder设置返回的集合
m.addAttribute("demands",demands);
//返回页面
return "demandhall";
}
由于session中的参数值为空(null),所以报错500 提示空指针异常
2.怎么解决
代码如下(示例):
if(demandProjectName !=null){
//调用service方法查询数据库
List<Demand> demands=demandService.demandprojecttype(prohectTypeId,demandProjectName);
//Moder设置返回的集合
m.addAttribute("demands",demands);
//返回页面
return "demandhall";
}else {
demandProjectName="";
List<Demand> demands=demandService.demandprojecttype(prohectTypeId,demandProjectName);
m.addAttribute("demands",demands);
return "demandhall";
}
可以在后台写个判断,如果为空的话重新设置值
<div class="layui-btn-container">
<%--<button type="submit" class="layui-btn">查询项目</button>--%>
<input type="hidden" value="${pageContext.request.contextPath}" id="contextPath">
<input type="button" class="layui-btn" id="withExport" value="查询项目">
</div>
也可以在前端设置value值或者设置一个隐藏的input标签