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

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 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader