Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Connection Pooling - null pointer exception

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

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.

==============================
SOFTWARE:
mysql 4.1.10
tomcat 4.0.1
mysql-connector-java-3.1.7
===============================

ERROR MESSAGE:

java.lang.NullPointerException
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() {
connectionPool.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.
try{
quantity = Integer.parseInt(quantityAsString);
if (quantity < 0) throw new NumberFormatException();
}
catch(NumberFormatException nfe){
quantity = 1;
}

Connection connection = connectionPool.getConnection(); // LINE 46
try{
Product product = ProductDB.readRecord(connection, productCode);
if (product != null){
LineItem lineItem = new LineItem(product, quantity);
if (quantity > 0)
cart.addItem(lineItem);
else
cart.removeItem(lineItem);
//throw new SQLException("test");
}
}
catch(SQLException e){
log("CartServlet SQLException: " + e);
throw new ServletException(e);
}
finally{
connectionPool.freeConnection(connection);
}
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.
else{
url = "/cart/cart.jsp";
}
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
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(){
try{
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,
"../webapps/musicStore/connectPool.log",
1.0);
}
}
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"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

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

<servlet>
<servlet-name>EmailServlet</servlet-name>
<servlet-class>music.email.EmailServlet</servlet-class>
</servlet>

<servlet>
<servlet-name>CartServlet</servlet-name>
<servlet-class>music.cart.CartServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>EmailServlet</servlet-name>
<url-pattern>email/email</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>CartServlet</servlet-name>
<url-pattern>cart/cart</url-pattern>
</servlet-mapping>

<!-- Enable basic authentication using the JDBC realm -->
<!--
<security-constraint>
<web-resource-collection>
<web-resource-name>Basic</web-resource-name>
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>service</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
-->

</web-app>
===========================
 
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

Shailesh
 
James Thomsonn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailesh,
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 ]
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!