麻烦老师帮我看下,为什么是空指针呢?

麻烦老师帮我看下,为什么是空指针呢?

http://img1.sycdn.imooc.com//climg/5bad624500013ec708850301.jpg

package com.imooc.sm.global;

import com.imooc.sm.entity.Log;
import com.imooc.sm.entity.Staff;
import com.imooc.sm.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Component
@Aspect
public class LogAdvice {
   @Autowired
   private LogService logService;

   @After("execution(* com.imooc.sm.controller.*.*(..)) && !execution(* com.imooc.sm.controller.SelfController.*(..))")
   public void operationLog(JoinPoint joinPoint) {
       Log log = new Log();
       log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
       log.setOperation(joinPoint.getSignature().getName());
       HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
       HttpSession session = request.getSession();
       Object obj = session.getAttribute("USER");
       Staff staff = (Staff) obj;
       log.setOperator(staff.getAccount());
       log.setResult("成功");

       logService.addOperationLog(log);

   }

   @AfterThrowing(throwing = "e", pointcut = "execution(* com.imooc.sm.controller.*.*(..))")
   public void systemLog(JoinPoint joinPoint, Throwable e) {
       Log log = new Log();
       log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
       log.setOperation(joinPoint.getSignature().getName());
       HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
       HttpSession session = request.getSession();
       Object obj = session.getAttribute("USER");
       Staff staff = (Staff) obj;
       log.setOperator(staff.getAccount());
       log.setResult(e.getClass().getSimpleName());
       logService.addSystemLog(log);
   }

   @After("execution(* com.imooc.sm.controller.SelfController.login(..))")
   public void loginLog(JoinPoint joinPoint) {
       Log log = new Log();
       log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
       log.setOperation(joinPoint.getSignature().getName());
       HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
       HttpSession session = request.getSession();
       Object obj = session.getAttribute("USER");
       if (obj == null) {
           log.setOperation(request.getParameter("account"));
           log.setResult("失败");
       } else {
           Staff staff = (Staff) obj;
           log.setOperator(staff.getAccount());
           log.setResult("成功");
       }
       logService.addLoginLog(log);
   }

   @Before("execution(* com.imooc.sm.controller.SelfController.logout(..))")
   public void logoutLog(JoinPoint joinPoint) {
       Log log = new Log();
       log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
       log.setOperation(joinPoint.getSignature().getName());
       HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
       HttpSession session = request.getSession();
       Object obj = session.getAttribute("USER");
       if (obj == null) {
           log.setOperation(request.getParameter("account"));
           log.setResult("失败");
       } else {
           Staff staff = (Staff) obj;
           log.setOperator(staff.getAccount());
           log.setResult("成功");
       }
       logService.addLoginLog(log);
   }
}
进不去个人中心哦

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

空指针异常,既获用null去调方法,或属性。那么,同学看到的是哪行报空指针异常呢?

从同学的截图看,

http://img1.sycdn.imooc.com//climg/5bad919d000126ec08960309.jpg

obj为null。那么,我们是在哪里向域中存的呢?

是在下图中的红框位置。


http://img1.sycdn.imooc.com//climg/5bad9176000115ca09260668.jpg

建议同学看一下,是否在下图的红框位置向域中设置了user 呢?如果设置了,建议在前边打印一句输出语句,看看是否执行到这儿了。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 广州麻辣牛杂档 提问者 #1
    哥,这一块的代码我贴出来了,麻烦帮我看下。 我弄了打印语句,结果是没有执行的。在没有添加日志功能之前,登录啥的都是正常的,个人中心修改编辑添加都能正常使用
    2018-09-28 10:55:30
  • 广州麻辣牛杂档 提问者 #2
    尴尬了 , 是数据库的账户名重叠了 。哈哈
    2018-09-28 11:13:32
提问者 广州麻辣牛杂档 2018-09-28 10:55:44
package com.imooc.sm.controller;

import com.imooc.sm.entity.Staff;
import com.imooc.sm.service.SelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller("selfController")
public class SelfController {
    @Autowired
    private SelfService selfService;

    public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }

    public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("index.jsp").forward(request, response);

    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");

        Staff staff = selfService.login(account, password);
        if (staff == null) {
            response.sendRedirect("toLogin.do");
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("USER", staff);
            response.sendRedirect("main.do");
            System.out.println(staff);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.setAttribute("USER", null);
        response.sendRedirect("toLogin.do");
    }

    public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../info.jsp").forward(request, response);

    }

    public void toChangePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../change_password.jsp").forward(request, response);

    }

    public void changePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");
        HttpSession session = request.getSession();
        Staff staff = (Staff) session.getAttribute("USER");
        if (!staff.getPassword().equals(password)) {
            response.sendRedirect("toChangePassword.do");

        } else {
            selfService.changePassword(staff.getId(), password1);
            response.sendRedirect("../logout.do");
            response.getWriter().print("<script type=\"text/javascript\">parent.location.href=\"../logout.do\"</script>");
            ;

        }
    }
}


问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师