This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

data source defined but not found..

 
Greenhorn
Posts: 26
Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i am writing a small smple application in spring having the following details:


package com.vaannila.domain;

import java.sql.Types;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;

public class UsertableDAOImpl implements UsertableDAO{

JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

//private DataFieldMaxValueIncrementer dataFieldMaxValueIncrementer;
public DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}

@Override
public void addUser(Usertable usertable) {
// TODO Auto-generated method stub
//get next autoincrement value
int i=0;
Integer id = new Integer(++i);//dataFieldMaxValueIncrementer.nextIntValue();
usertable.setUserid(id.toString());
String sql = "insert into Usertable (userid, name, password,gender,country,aboutyou,mailinglist,community) values (?,?,?)";
Object args []= new Object[] { id.toString(), usertable.getName(), usertable.getPassword(),usertable.getGender(),usertable.getCountry(),usertable.getMailinglist(),usertable.getCommunity(),usertable.getAboutyou() };
int types[] = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR ,Types.VARCHAR, Types.VARCHAR, Types.VARCHAR ,Types.VARCHAR,Types.VARCHAR};
//DataSource da=new Da
//JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
jdbcTemplate.update(sql, args, types);//Exception line----------------------------------------------
}

@Override
public void deleteUser(Usertable usertable) {
// TODO Auto-generated method stub

}

@Override
public List<Usertable> getUsers(Usertable usertable) {
// TODO Auto-generated method stub
return null;
}

@Override
public void updateUser(Usertable user) {
// TODO Auto-generated method stub
}

// public void setDataFieldMaxValueIncrementer(
// DataFieldMaxValueIncrementer dataFieldMaxValueIncrementer) {
// this.dataFieldMaxValueIncrementer = dataFieldMaxValueIncrementer;
// }

public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
}




dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messeges" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

<bean id="userService" class="com.vaannila.service.UserServiceImpl" >

</bean>

<bean name="/userRegistration.htm" class="com.vaannila.web.UserController" p:userService-ref="userService" p:formView="userForm" p:successView="userSuccess" p:validator-ref="userValidator" />

<bean id="userValidator" class="com.vaannila.validator.UserValidator" />



<bean id="usertableDAO" class="com.vaannila.domain.UsertableDAOImpl" >
<property name="dataSource" ref="dataSource"/>
<!-- <property name="jdbcTemplate" ref="jdbcTemplate"/>-->
<!-- <property name="dataFieldMaxValueIncrementer"> <ref bean="dataFieldMaxValueIncrementer" />-->
<!-- </property>-->
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref bean="dataSource" />
</constructor-arg>
</bean>

<!-- <bean id="dataFieldMaxValueIncrementer" class="org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer">-->
<!-- <constructor-arg>-->
<!-- <ref bean="dataSource" />-->
<!-- </constructor-arg>-->
<!-- <constructor-arg><value>book_id_seq</value> </constructor-arg>-->
<!-- </bean>-->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="kunal" />
<property name="password" value="kunal" />
</bean>

</beans>

Exception
SEVERE: Servlet.service() for servlet dispatcher threw exception
java.lang.NullPointerException
at com.vaannila.domain.UsertableDAOImpl.addUser(UsertableDAOImpl.java:34)
at com.vaannila.service.UserServiceImpl.add(UserServiceImpl.java:23)
at com.vaannila.web.UserController.onSubmit(UserController.java:27)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:409)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:381)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.ap

please help
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please use code tags while posting to a forum, without code tags, it is very difficult to read, can you please edit this one, which is the 34 line that is throwing the error?
 
kunal green
Greenhorn
Posts: 26
Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the line which is throwing exception is
jdbcTemplate.update(sql, args, types);//Exception line----------------------------------------------

it appears that datasource is not injected properly but i verified it from the dispatcher-servlet.xml i might be wrong but it appears correct to me.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kunal green wrote:is the line which is throwing exception is
jdbcTemplate.update(sql, args, types);//Exception line----------------------------------------------

it appears that datasource is not injected properly but i verified it from the dispatcher-servlet.xml i might be wrong but it appears correct to me.



That might be your problem. If you define the datasource in your dispatcher servlet.xml and it gets created by the DispatcherServlet, but you other classes is in the ApplicationContext created by ContextLoaderListener, then you will not be able to inject the dataSource into those classes.

Basically in a Spring MVC app you create two ApplicationContexts. One for the middle tier, your services/repositories/dataSource/transactionManager. The other ApplicationContext from the dispatcherServlet should only contain Web Layer beans like Controllers, View Resolvers and MappingHandlers

So with the two app contexts. the web layer can see the beans in the middle tier, but not the other way around.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic