jakubee 0 Newbie Poster

Hello everyone,
I dont know if im placing this in a good place.
I have a problem with my application. Its a simple application with connecting to database, retriving info and populating table.
I changed the apache tomcats context.xml to looks like this:

<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="root" password="MY_PASSWORD" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/test"/>

****My jsp look like this: ****

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*" %>
<%@ page import="business.Module" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body> 


<table border=1 bgcolor="lightgrey">
<tr> <th>ID</th><th>Code</th><th>Description</th><th>Lecturer</th>
<c:forEach var="module" items="${sessionScope.modules}">
    <tr>
      <td><c:out value="${module.id}" /></td>
      <td><c:out value="${module.code}" /></td>
      <td><c:out value="${module.description}" /></td>   
      <td><c:out value="${module.lecturer}" /></td>      
    </tr>  
</c:forEach>
</table>
<form action="TestConnectionServlet" method="post">
        <input type="hidden" name="action" value="populate" />
        <input type="submit" value="populate table" />

</form>

</body>
</html>

After i run this i get the errors:

SEVERE: Servlet.service() for servlet [servlet.TestConnectionServlet] in context with path [/Database_Connection_Pooling_-_Exercise1] threw exception
java.lang.NullPointerException**
at dao.ModuleDao.getAllModules(ModuleDao.java:28)
at servlet.TestConnectionServlet.doPost(TestConnectionServlet.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Im using the dao class, moduleDao and testConnectionServlet.

The code that error refers to
ModuleDao:

public class ModuleDao extends Dao {

public List<Module> getAllModules() throws DaoException {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    List<Module> modules = new ArrayList<Module>();

    try {
        //Get connection object using the methods in the super class (Dao.java)...
        con = this.getConnection();

        String query = "SELECT * FROM module";
        ps = con.prepareStatement(query);            <<<<<<<<<<-------- this is line 28

        //Using a PreparedStatement to execute SQL...
        rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            String code = rs.getString("code");
            String description = rs.getString("description");
            String lecturer = rs.getString("lecturer");

            Module m = new Module(id,code,description,lecturer);
            modules.add(m);
        }
    } catch (SQLException e) {
        throw new DaoException("getAllModules()" + e.getMessage());
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                freeConnection(con);
            }
        } catch (SQLException e) {
            throw new DaoException(e.getMessage());
        }
    }

    return modules;
    }

And TestConnectionServlet:

/**
 * Servlet implementation class TestConnectionServlet
 */
@WebServlet("/TestConnectionServlet")
public class TestConnectionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * Default constructor. 
 */
public TestConnectionServlet() {
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if ( request.getParameter("action").equalsIgnoreCase("populate") ){
        String forwardToJsp = "";
        HttpSession session = request.getSession();
        ModuleDao mDao = new ModuleDao();

            List<Module> modules = new ArrayList<Module>();
            try {
                modules = mDao.getAllModules();     <<<---- this is line 53
            } catch (DaoException e) {
                e.printStackTrace();
            }

            session.setAttribute("modules", modules);
            forwardToJsp = "/TestConnection.jsp";   


        forwardToPage(request, response, forwardToJsp);
    }
}

Im struggling with this whole day now and cant get it working. Any ideas what might be wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.