Code is something like
2: <%
3: String rolename = (String)session.getAttribute("roles");
4: out.println("role is " + rolename);
5: if(rolename.equals("MY_ROLE")){
6: out.println("tension");
7: }
8: %>
问题是:第4行正确打印了rolename的值,即“MY_ROLE”,但我在第5行得到空指针异常,我假设因为rolename为null。
Error is as below :
org.apache.jasper.JasperException:在第10行处理JSP页面/WEB-INF/views/fragments/menu.jspf时发生异常
7: <%
8: String rolename = (String)session.getAttribute("roles");
9: out.println("role is " + rolename);
10: if(rolename.equals("MY_ROLE")){
11: out.println("tension");
12: }
13:%>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
org.apache.jsp.WEB_002dINF.views.pages.view_jsp._jspService(view_jsp.java:106)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Please help.
答案 0 :(得分:2)
执行String rolename = (String)session.getAttribute("roles");
时,会话参数可以是null
,也就是说,它可能从未设置过。
如果是这种情况,那么rolename
现在将null
和if(rolename.equals("MY_ROLE")){
将抛出NullPointerException,因为您在null变量上使用点运算符,这基本上等同于执行{ {1}}。
有几种方法可以解决它:
null.equals("MY_ROLE")
if(rolename!=null && rolename.equals("MY_ROLE")){
也就是说,要么显式检查null,要么颠倒比较的顺序,并对字符串文字执行点运算符。