pooya
pooya

Reputation: 135

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. 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

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>

Upvotes: 0

Views: 311

Answers (1)

Troley
Troley

Reputation: 1658

You are using the pol member variable in this line List<student> student = pol.getstudent(); in dispatch#listteacher method, but it has not yet been initialized. 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.

Also consider to name your classes kebab-cased, e.g. Dispatch instead of dispatch. That is the Java class naming convention.

Upvotes: 1

Related Questions