• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Connection Pooling - null pointer exception

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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(CartServlet.java:46)
at music.cart.CartServlet.doGet(CartServlet.java:84)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:446)
at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

FILE: CartServlet.java

package music.cart;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import music.business.*;
import music.data.*;
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);
FILE: MurachPool.java

package music.util;

import com.javaexchange.dbConnectionBroker.*;
import java.io.*;

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("org.gjt.mm.mysql.Driver",
"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 -->

Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FILE: MurachPool.java

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

James Thomsonn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
It will give me the powers of the gods. Not bad for a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic