File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Connection Pooling - null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Connection Pooling - null pointer exception" Watch "Connection Pooling - null pointer exception" New topic

Connection Pooling - null pointer exception

James Thomsonn

Joined: Apr 28, 2005
Posts: 2

I'm new to servlet/JSP programming, and especially connection pooling. I get a null pointer exception when I try to connect to my mysql database. The driver name, user name, and password for the database all work properly when connecting without connection pooling.

Any assistance you can provide would be greatly appreciated. Please let me know if there are any additional files you'd like me to post.

mysql 4.1.10
tomcat 4.0.1


at music.cart.CartServlet.doPost(
at music.cart.CartServlet.doGet(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.servlets.InvokerServlet.serveRequest(
at org.apache.catalina.servlets.InvokerServlet.doGet(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardPipeline.invokeNext(
at org.apache.catalina.core.StandardPipeline.invoke(
at org.apache.catalina.core.ContainerBase.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardPipeline.invokeNext(
at org.apache.catalina.valves.CertificatesValve.invoke(
at org.apache.catalina.core.StandardPipeline.invokeNext(
at org.apache.catalina.core.StandardPipeline.invoke(


package music.cart;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import music.util.*;

public class CartServlet extends HttpServlet{
private MurachPool connectionPool;

public void init() throws ServletException{
connectionPool = MurachPool.getInstance();

public void destroy() {

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{

String productCode = request.getParameter("productCode");
String quantityAsString = request.getParameter("quantity");
HttpSession session = request.getSession(true);

Cart cart = (Cart) session.getAttribute("cart");
if (cart == null){
cart = new Cart();
session.setAttribute("cart", cart);
int quantity = 1;
// If the user enters a negative or invalid number in the
// Update text box, the quantity is automatically reset to 1.
quantity = Integer.parseInt(quantityAsString);
if (quantity < 0) throw new NumberFormatException();
catch(NumberFormatException nfe){
quantity = 1;

Connection connection = connectionPool.getConnection(); // LINE 46
Product product = ProductDB.readRecord(connection, productCode);
if (product != null){
LineItem lineItem = new LineItem(product, quantity);
if (quantity > 0)
//throw new SQLException("test");
catch(SQLException e){
log("CartServlet SQLException: " + e);
throw new ServletException(e);
session.setAttribute("cart", cart);

// If no items exist in cart, forward to the Index page.
String url = "";
if (cart.getItems().size() <= 0){
url = "/cart/index.jsp";
// Otherwise, forward to the Cart page.
url = "/cart/cart.jsp";
RequestDispatcher dispatcher =
dispatcher.forward(request, response);

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
doPost(request, response);

package music.util;

import com.javaexchange.dbConnectionBroker.*;

public class MurachPool extends DbConnectionBroker{

private static MurachPool pool = null;

private MurachPool(String driverName, String dbUrl, String username,
String password, int minConns, int maxConns,
String logFilename, double maxConnTimeInDays)
throws IOException{
super(driverName, dbUrl, username, password,
minConns, maxConns, logFilename, maxConnTimeInDays);

public synchronized static MurachPool getInstance(){
if (pool == null){
//System.out.println("Creating the MurachPool object.");
pool = new MurachPool("",
"jdbc:mysql://localhost/musicTest", "root",
"", 5, 20,
catch(IOException ioe){
System.err.println("MurachPool IOException: " + ioe);
return pool;
FILE: web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"


<!-- Enable servlet mapping -->
<!-- To use these, you would need to call the mapped URL pattern from a JSP or servlet -->





<!-- Enable basic authentication using the JDBC realm -->

Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1082


did you check this if there is any message printed out
your statcktrace points to
which is Connection connection = connectionPool.getConnection();
and this line can throw null pointer exception IFF connectionPool null


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
James Thomsonn

Joined: Apr 28, 2005
Posts: 2
Thanks for your help. I checked catalina.out and found incorrect write permissions on the connectionlog file that I'd try to pass as an argument. Everything works now. Thanks again.
[ April 28, 2005: Message edited by: James Thomsonn ]
I agree. Here's the link:
subject: Connection Pooling - null pointer exception
It's not a secret anymore!