简体   繁体   English

使用 servlet 从 mysql 检索数据的空指针异常

[英]Null pointer exception for Retrieving Data from mysql using servlet

i am trying to Retrieving Data from my data base named id and it has a 2 columns uname and pass.我正在尝试从名为 id 的数据库中检索数据,它有 2 列 uname 和 pass。 i believe my beans class which is student is fine totally and as you can see pol class is for interacting with db and class dispach send the data to jsp,my problem is than when i am runing dispach on server i get the NullPointerException我相信我的 beans 类是学生完全没问题,正如你所看到的 pol 类用于与 db 交互,类 dispach 将数据发送到 jsp,我的问题是当我在服务器上运行 dispach 时,我得到 NullPointerException

 public class pol { public List<student> getstudent() throws Exception { List<student> students = new ArrayList<>(); String connectionURL = "jdbc:mysql://localhost:3306/pooya"; Connection connection = null; Statement s = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionURL, "root", ""); String sql = "select * from id"; s = connection.createStatement(); s.executeQuery(sql); rs = s.getResultSet(); while (rs.next()) { String uname = rs.getString("uname"); String pass = rs.getString("pass"); student tempstudent = new student(uname, pass); students.add(tempstudent); } return students; } finally { // close JDBC objects close(connection, s, rs); } } private void close(Connection connection, Statement s, ResultSet rs) { try { if (rs != null) { rs.close(); } if (s != null) { s.close(); } if (connection != null) { connection.close(); // doesn't really close it ... just puts back in connection pool } } catch (Exception exc) { exc.printStackTrace(); } } }

 public class dispach extends HttpServlet { private java java; private pol pol; @Resource(name = "jdbc/web_student_tracker") private DataSource dataSource; @Override public void init() throws ServletException { try { java = new java(dataSource); } catch (Exception exc) { throw new ServletException(exc); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { listteacher(request, response); } catch (Exception e) { e.printStackTrace(); } } private void listteacher(HttpServletRequest request, HttpServletResponse response) throws Exception { List<student> student = pol.getstudent(); request.setAttribute("select", student); //fix RequestDispatcher dispatcher = request.getRequestDispatcher("/NewFile.jsp"); dispatcher.forward(request, response); } }

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <table> <c:forEach var="tempstudent" items="${select}"> <tr> <td>${tempstudent.uname}</td> </tr> </c:forEach> </table> </body> </html>

You are using the pol member variable in this line List<student> student = pol.getstudent();您在这一行中使用了pol成员变量List<student> student = pol.getstudent(); in dispatch#listteacher method, but it has not yet been initialized.dispatch#listteacher方法中,但尚未初始化。 You have to initialize it first (in constructor, or some other way) to be able to call methods on it, else you will indeed get a NullPointerException .您必须首先初始化它(在构造函数中,或以其他方式)才能对其调用方法,否则您确实会得到NullPointerException

Also consider to name your classes kebab-cased, eg Dispatch instead of dispatch .还要考虑将您的类命名为 kebab-cased,例如Dispatch而不是dispatch That is the Java class naming convention.这就是 Java 类命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM