I'm building a Movie Rating CRUD application and whenever I click on the 'Categories' link, which is supposed to access the database and display the categories and the list of movies available, I receive an error message:
java.lang.NullPointerException
Database.ConnectionPool.freeConnection(ConnectionPool.java:40)
Database.ItemDB.getItems(ItemDB.java:59)
Controllers.CatalogController.processRequest(CatalogController.java:33)
Controllers.CatalogController.doGet(CatalogController.java:86)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
ItemDB.java
public static ArrayList<Item> getItems() {
ArrayList<Item> items = new ArrayList<Item>();
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM Item";
try {
ps = connection.prepareStatement(query);
rs = ps.executeQuery();
while(rs.next()) {
Item item = new Item();
item.setItemCode(rs.getString("itemCode"));
item.setItemName(rs.getString("itemName"));
item.setCatalogCategory(rs.getString("CatalogCategory"));
item.setItemDescription(rs.getString("descript"));
item.setItemRating(rs.getString("itemRating"));
item.setImageUrl(rs.getString("imageURL"));
items.add(item);
}
return items;
} catch (SQLException e) {
System.out.println(e);
return null;
} finally {
DBUtil.closeResultSet(rs);
DBUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
}
ConnectionPool.java
public void freeConnection(Connection c) {
try {
c.close();
} catch (SQLException e) {
System.out.println(e);
}
}
Context.xml
<Resource
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
logAbandoned="true"
maxActive="100" maxIdle="30" maxWait="10000"
name="jdbc/MovieThoughts" removeAbandoned="true"
removeAbandonedTimeout="60" type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/root"
username="MovieThoughts" password=[my_password] />
I've tested the connection in my MySQL workbench and it can establish the connection in there.