| Author |
Struts, ''dataSource'' is null
|
Chan Yan
Greenhorn
Joined: Oct 21, 2005
Posts: 15
|
|
Hello. I am working on a small projest using Struts and defining JNDI to get datasource. In the login page, when 'submit' is clicked, I get error messages: WARN : 2007-10-01 13:52:34,658 [http-8080-Processor25] RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException ERROR: 2007-10-01 13:52:34,758 [http-8080-Processor25] [action] - Servlet.service() for servlet action threw exception java.lang.NullPointerException at com.swlAdminJ.bo.AdminLoginBO.findByName(AdminLoginBO.java:31) at com.swlAdminJ.action.login.LoginSubmitAction.execute(LoginSubmitAction.java:32) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) at javax.servlet.http.HttpServlet.service(HttpServlet.java:709) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) ....... I found that the 'dataSource' in 'findByName(String aName, String aPwd)' of class 'AdminLoginBO' was null. I can't figure out why this dataSource is null. Does anyone have idea what causes the error? Thanks in advance. Here are part of the code. I use Tomcat 5.5, struts 1.1, mysql 5.0 and Eclipse. 'mysql-connector-java-5.0.7-bin.jar' is under '..\Tomcat5.5\common\lib'. context.xml under 'WebRoot\META-INF' is: <?xml version="1.0" encoding="UTF-8"?> <Context > <Resource auth="Container" description="DB Connection" name="jdbc/myDataSource" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" maxIdle="2" maxWait="5000" username="root" password="root" url="jdbc:mysql://localhost:3306/my_db?relaxAutoCommit=true" validationQuery="select 1" maxActive="4"/> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>META-INF/context.xml</WatchedResource> </Context> --------------------- web.xml (part of it) <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/myDataSource</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <servlet> ..... </servlet> </web-app> ----------------- Login action class (when 'submit'button is clicked, this one is executed): public class LoginSubmitAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward returnForward = null; AdminLoginBO curAdminBO = AdminLoginBO.getInstance(); String userId = (String)((DynaValidatorForm)form).get("userId"); String pwd = (String)((DynaValidatorForm)form).get("password"); AdminLogin curAdmin = curAdminBO.findByName(userId, pwd); ActionMessages errors = new ActionMessages(); HttpSession session = request.getSession(true); if(curAdmin != null) { session.setAttribute("adminLogin", curAdmin); returnForward = mapping.findForward("success"); } else .... } } -------------- AdminLoginBO.java (part of it) public class AdminLoginBO extends BaseLinkBO { private static DataSource dataSource; AdminLoginBO(DataSource dataSource) { super(dataSource); } AdminLoginBO() { super(); } public AdminLogin findByName(String aName, String aPwd) throws EntityNotFoundException, SQLException { Connection conn = dataSource.getConnection(); try { AdminLogin curAdmin = (new AdminLoginDAO(conn)).findByName(aName, aPwd); conn.commit(); return curAdmin; } catch (EntityNotFoundException e) { conn.rollback(); throw e; } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } .... } -------------------- BaseLinkBO.java (part of it) public abstract class BaseLinkBO { protected final DataSource dataSource; protected BaseLinkBO(DataSource dataSource) { this.dataSource = dataSource; } protected BaseLinkBO() { this.dataSource = ServiceLocator.getInstance().getDataSource(); } ...... } ----------- ServiceLocator.java public class ServiceLocator { private static ServiceLocator instance; private DataSource dataSource; static { instance = new ServiceLocator(); } private ServiceLocator () { Context context = null; try { context = new InitialContext(); dataSource = (DataSource) context.lookup("java:/comp/env/jdbc/myDataSource"); } catch (NamingException e) { System.out.println(e.toString()); } } public static ServiceLocator getInstance() { return instance; } public DataSource getDataSource() { return dataSource; } } [ October 01, 2007: Message edited by: northcloud ]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56529
|
|
|
"northcloud", please check your private messages for an important message.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
|
|
Please use code tags in the future! Is the private static dataSource variable hiding the one in the superclass (BaseLinkBO)?
|
 |
Chan Yan
Greenhorn
Joined: Oct 21, 2005
Posts: 15
|
|
Thank you, Carol Enderlin. This is my mistake (I didn't notice it, so silly). Once I take off 'private static DataSource dataSource;' from class AdminLoginBO, it works. Thank you again. I am sorry I didn't use 'code' tag. I won't forget it for sure.
Originally posted by Carol Enderlin: Please use code tags in the future! Is the private static dataSource variable hiding the one in the superclass (BaseLinkBO)?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56529
|
|
"northcloud", I see that you have read my message regarding adjusting your display name to meet JavaRanch standards, but have ignored it. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post. Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. Be aware that accounts with invalid display names are disabled. bear JavaRanch Sheriff [ October 02, 2007: Message edited by: Bear Bibeault ]
|
 |
 |
|
|
subject: Struts, ''dataSource'' is null
|
|
|