今天想到用timer写一个定时清空session值得定时器,但报空指针。
代码如下:
public void removeYZM( HttpServletRequest request, String code) {
Timer timer = new Timer();
System.out.println("验证码:------"+request.getSession().getAttribute(code));
timer.schedule(new TimerTask() {
public void run() {
// 删除session中存的验证码
request.getSession().removeAttribute(code);
timer.cancel();
}
}, 1 * 60 * 1000);
}
报错信息
Exception in thread "Timer-1" java.lang.NullPointerException
at com.temp.ssm.controller.UserClientController$1.run(UserClientController.java:51)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)