I'm using Spring 3 with Hibernate
But when I trying to save data in database it giving me nullPointerException
<pre>
java.lang.NullPointerException
com.ivalix.services.LoginServiceImpl.add(LoginServiceImpl.java:27)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
</pre>
My dispatcher-servlet.xml code is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.ivalix.controller" />
<context:property-placeholder location="classpath:jdbc.properties" />
<tx:annotation-driven transaction-manager="hibernatetransactionManager" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
p:basename="views" />
<bean id="loginService" class="com.ivalix.services.LoginServiceImpl" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driver}"
p:url="${jdbc.url}" p:username="${jdbc.user}"
p:password="${jdbc.password}" p:maxActive="0" p:initialSize="50" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.ivalix.dto.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.show_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernatetransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
p:definitions="/WEB-INF/tiles-defs.xml" />
<bean id="userValidator" class="com.ivalix.validator.UserValidator" />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="validation" />
</beans>
Controller file is:
<pre>
package com.ivalix.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.ivalix.services.LoginService;
import com.ivalix.validator.UserValidator;
import com.ivalix.dto.User;
@Controller
@RequestMapping("/login.htm")
@SessionAttributes("user")
public class LoginController {
private LoginService loginService;
private UserValidator userValidator;
@Autowired
public void setUserService(LoginService loginService, UserValidator userValidator) {
this.loginService = loginService;
this.userValidator = userValidator;
}
@RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model) {
User user = new User();
model.addAttribute("user", user);
return "loginForm";
}
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@ModelAttribute(value="user") User user,BindingResult result) {
userValidator.validate(user, result);
if (result.hasErrors()) {
return "loginForm";
} else {
loginService.add(user);
return "redirectLoginSuccess";
}
}
}
</pre>
ServiceImpl is:
<pre>
package com.ivalix.services;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.ivalix.dao.UserDao;
import com.ivalix.dao.impl.UserDaoImpl;
import com.ivalix.dto.User;
@Transactional
public class LoginServiceImpl implements LoginService {
@Autowired
private static UserDao userDao;
LoginServiceImpl() {
//userDao = UserDaoImpl.getInstance();
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void add(User user) {
userDao.saveUser(user);
}
}
</pre>
DaoImpl is:
<pre>
package com.ivalix.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ivalix.dao.UserDao;
import com.ivalix.dto.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
private static UserDao userDao;
public static UserDao getInstance() {
if (userDao != null)
return userDao;
else {
userDao = new UserDaoImpl();
return userDao;
}
}
// To Save the user detail
public void saveUser(User user) {
sessionFactory.getCurrentSession().saveOrUpdate(user);
}
// To get list of all user
@SuppressWarnings("unchecked")
public List<User> listUsers() {
return (List<User>) sessionFactory.getCurrentSession().createCriteria(User.class).list();
}
}
</pre>
UserValidator Bean:
package com.ivalix.validator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.ivalix.dto.User;
public class UserValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return User.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
User user = (User) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "email.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required");
if (!isValidEmailAddress(user.getEmail())) errors.rejectValue("email",
"email.invalid");
}
public boolean isValidEmailAddress(String emailAddress){
String expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = emailAddress;
Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}
}
List of jar I used:
</pre>
- antlr-2.7.6.jar
- aopalliance.jar
- appserv-jstl.jar
- asm-1.5.3.jar
- asm-attrs-1.5.3.jar
- c3p0-0.9.1.jar
- cglib-2.1_3.jar
- cglib-nodep-2.1_3.jar
- commons-beanutils-1.8.3.jar
- commons-codec-1.3.jar
- commons-collections-3.2.1.jar
- commons-dbcp-1.4.jar
- commons-digester-2.1.jar
- commons-fileupload-1.2.2.jar
- commons-io-1.2.jar
- commons-lang-2.6.jar
- commons-logging-1.1.1.jar
- commons-pool-1.5.5.jar
- displaytag-1.2.jar
- displaytag-export-poi-1.2.jar
- displaytag-portlet-1.2.jar
- dom4j-1.6.1.jar
- ejb3-persistence-1.0.1.GA.jar
- hibernate-3.2.6.ga.jar
- hibernate-annotations-3.4.0.GA.jar
- hibernate-commons-annotations-3.1.0.GA.jar
- hibernate-core-3.3.2.GA.jar
- javassist-3.12.0.GA.jar
- jstl-1.2.jar
- jta-1.1.jar
- mysql-connector-java-5.1.6.jar
- org.apache.servicemix.bundles.commons-io-1.3.2_1.jar
- org.springframework.aop-3.0.5.RELEASE.jar
- org.springframework.asm-3.0.5.RELEASE.jar
- org.springframework.aspects-3.0.5.RELEASE.jar
- org.springframework.beans-3.0.5.RELEASE.jar
- org.springframework.context.support-3.0.5.RELEASE.jar
- org.springframework.context-3.0.5.RELEASE.jar
- org.springframework.core-3.0.5.RELEASE.jar
- org.springframework.expression-3.0.5.RELEASE.jar
- org.springframework.instrument.tomcat-3.0.5.RELEASE.jar
- org.springframework.instrument-3.0.5.RELEASE.jar
- org.springframework.jdbc-3.0.5.RELEASE.jar
- org.springframework.jms-3.0.5.RELEASE.jar
- org.springframework.orm-3.0.5.RELEASE.jar
- org.springframework.oxm-3.0.5.RELEASE.jar
- org.springframework.test-3.0.5.RELEASE.jar
- org.springframework.transaction-3.0.5.RELEASE.jar
- org.springframework.web.portlet-3.0.5.RELEASE.jar
- org.springframework.web.servlet-3.0.5.RELEASE.jar
- org.springframework.web.struts-3.0.5.RELEASE.jar
- org.springframework.web-3.0.5.RELEASE.jar
- servlet-api.jar
- slf4j-api-1.6.1.jar
- spring-modules-validation.jar
- spring-security-acl-3.0.5.RELEASE.jar
- spring-security-config-3.0.5.RELEASE.jar
- spring-security-core-3.0.5.RELEASE.jar
- spring-security-taglibs-3.0.5.RELEASE.jar
- spring-security-web-3.0.5.RELEASE.jar
- tiles-api-2.2.2.jar
- tiles-core-2.2.2.jar
- tiles-jsp-2.2.2.jar
- tiles-servlet-2.2.2.jar
- tiles-template-2.2.2.jar
- urlrewrite-2.6.0.jar
- validation-api-1.0.0.GA.jar
- xalan.jar
- xercesImpl-2.9.1.jar
- xml-apis.jar
</pre>