weixin_45992860 2021-05-20 20:42 采纳率: 80%
浏览 216
已采纳

java.lang.NullPointerException

UserDao.java

package com.sys.dao;


import java.sql.*;
import java.sql.ResultSet;



import com.sys.entity.User;
import com.sys.util.DBConnectionPool;

public class UserDao {
public User login(String username,String password) {
    Connection   conn = DBConnectionPool.getInstance().getConn();
    PreparedStatement ps;
    ResultSet  rs;
    User  user = new User();
    System.out.println("username"+username);
    System.out.println("password"+password);
    try {
        ps = conn.prepareStatement("select * from login where username=? and password=?");//报空指针的语句
        ps.setString(1,username);
        ps.setString(2,password);

        rs = ps.executeQuery();
        if(rs.next()){
            user = new User();
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setSchool(rs.getString("school"));
            user.setRole(rs.getString("role"));
            user.setName(rs.getString("name"));

        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DBConnectionPool.getInstance().releaseConn(conn);
    return user;
}

}

LoginServlet.java

package com.sys.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sys.dao.UserDao;
import com.sys.entity.User;
public class LoginServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        //获取用户名和密码
        String username = request.getParameter("username");

        String password = request.getParameter("password");
        if(password==null||username==null){
            System.out.println("Error");
        }
        //获取UserDao实例
        UserDao userDao = new UserDao();

        User user = userDao.login(username,password);//报空指针异常的语句

        HttpSession session=request.getSession(true);
        session.setAttribute("user",user);
        // 判断user是否为空
        if(user != null){
            // 转发到LoginSuccess.jsp页面
            //  getRequestDispatcher()是请求转发
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request, response);
        }else{
            // 登录失败
            request.getRequestDispatcher("/WEB-INF/pages/Login.jsp").forward(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);//报空指针的异常的语句
    }

}

在登陆后一段时间,就自动报出上面这个错误,刷新页面也回不到原来的界面了,怎么解决这个问题

  • 写回答

6条回答 默认 最新

  • CSDN专家-sinJack 2021-05-20 21:04
    关注

    如有帮助,还望采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 cpuid是如何转换的
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件