异常描述:空值异常,java.lang.NullPointerException 这是因为我自己在拦截器拦截的时候进行的登录判断时通过session获取到一个空的user并使用这个实体类的getUserName方法获取用户名出现了错误,这是因为我还没有登录,并没有session保存所以出现了异常。这是程序本身设计错误,当然有时候其他的可能也会出现空值异常。
严重: Servlet.service() for servlet [dispatcherServlet] in context with path [/ssm-demo2] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at com.liangfufeng.interceptor.LoginInterceptor.preHandle(LoginInterceptor.java:40) at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:134) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:958) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722)出现错误没修改之前的代码如下:
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //创建session HttpSession session=request.getSession(); //无需登录可以访问的地址 String [] allowUrls=new String[]{"toLogin","/login"}; //获取当前请求地址 String url=request.getRequestURL().toString(); //获取session中的用户 User user=(User) session.getAttribute("user"); System.out.println(user); //判断是否已经登录 for (String strUrl : allowUrls) { if(url.contains(strUrl)) { return true; } } if(user.getUserName()==null){ request.getRequestDispatcher("/login").forward(request, response); return false; } return true; }错误出现在:
user.getUserName()==null修改思路: 直接使用user作判断而不是使用的的userName属性
user==null修改后的代码如下:
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //创建session HttpSession session=request.getSession(); //无需登录可以访问的地址 String [] allowUrls=new String[]{"toLogin","/login"}; //获取当前请求地址 String url=request.getRequestURL().toString(); //获取session中的用户 User user=(User) session.getAttribute("user"); System.out.println(user); //判断是否已经登录 for (String strUrl : allowUrls) { if(url.contains(strUrl)) { return true; } } if(user==null){ request.getRequestDispatcher("/login").forward(request, response); return false; } return true; }