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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nullpointer exception while accessing datasource in struts

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Ranchites ,

When I call method getDataSource(req) in my struts application, I am getting the following NULLPOINTER EXCEPTION.Suggest me a workaround for the same.


Regards

-------------------------Error---------------------


java.lang.NullPointerException
org.apache.struts.action.Action.getDataSource(Action.java:323)
org.apache.struts.action.Action.getDataSource(Action.java:304)
com.patil.DataBase.getConnected(DataBase.java:15)
com.patil.BusinessLogic.dbUpdate(BusinessLogic.java:14)
com.patil.BankAction.execute(BankAction.java:16)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
you have given too little info to go on. Can you post the code fragments?
 
Shaan patil
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

The following is the definition of datasource in the struts-config.xml
.

<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="com.mysql.jdbc.Driver"/>
<set-property
property="url"
value="jdbc:mysql:///struts"/>
<set-property
property="username"
value="root"/>
<set-property
property="password"
value="omshanthi"/>
<set-property
property="maxActive"
value="10"/>
<set-property
property="maxWait"
value="5000"/>
<set-property
property="defaultAutoCommit"
value="false"/>
<set-property
property="defaultReadOnly"
value="false"/>

</data-source>
</data-sources>

---The following is the class in which I am calling the getDataSource method---

public class DataBase extends Action{

public Connection getConnected(HttpServletRequest req) {
Connection connection = null;
DataSource ds = getDataSource(req);
try
{
connection=ds.getConnection();
}
catch(Exception s)
{
System.out.println("The SQL Exception caught in the DataBase class");
}
finally
{
return (connection);
}


BusinessLogic Class from which I am calling the getConnected method

package com.patil;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

public class BusinessLogic {

public ResultBean dbUpdate(ActionForm form,HttpServletRequest req)

{
Connection connection=new DataBase().getConnected(req);
System.out.println(" *************The connection reference got************* ");
ResultBean resultBean=null;
return resultBean;


}

}
[ March 21, 2008: Message edited by: Shaan patil ]
 
author & internet detective
Posts: 41871
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Let's discuss this in one thread
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic