Results 1 to 9 of 9

Thread: dao.save(object) - java.lang.NullPointerException

  1. #1
    Join Date
    May 2010
    Posts
    18

    Default dao.save(object) - java.lang.NullPointerException

    Hi,
    I'm trying to implement object presistence on my simple hibernate project. I have a bean called (Patient) and a daoImp (PatientDaoImp) to store the patient attributes into the DB. When I'm running the test everything looks fine with out any problem, but when i'm trying to post patient information from an HTML form the line dao.save(patient) returns null pointer exception. any idea why? thanks.

    HTML Code:
    [U]app-context.xml[/U]
    
    <bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    		p:driverClassName="${dataSource.driverClassName}" p:url="${dataSource.url}"
    		p:username="${dataSource.username}" p:password="${dataSource.password}" />
    	
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="packagesToScan" value="com.pems.domain" />
    		<property name="schemaUpdate" value="true" />
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.format_sql">true</prop>
    				<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
    			</props>
    		</property>
    	</bean>
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    PatientDaoImp
    HTML Code:
    @ContextConfiguration("/META-INF/spring/app-context.xml")
    public class PatientDaoImp {
    	@Autowired
    	private SessionFactory sessionFactory;
    	
    	public void save(Patient p){
    		Session session = sessionFactory.getCurrentSession();
    		session.save(p);
    		session.flush();
    	}
    PatientController
    HTML Code:
    @Controller
    @RequestMapping("/patient/*")
    public class PatientController {
    	PatientDaoImp dao;
    	
    	@RequestMapping("create")
    	public String createPatientForm(Model model){
    		model.addAttribute("patient",new Patient());
    		return "create";
    	}
    	@RequestMapping(value="add",method = RequestMethod.POST)
    	public String createPatient(Patient patient, BindingResult result, Model model){
    		System.out.println("create patient called!");
    		model.addAttribute("patient", patient);
    		dao.save(patient);
    		return "redirect:create";	
    	}
    }
    Null pointer exception
    HTML Code:
    java.lang.NullPointerException
    	com.pems.web.PatientController.createPatient(PatientController.java:28)
    	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	java.lang.reflect.Method.invoke(Method.java:597)
    	org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
    	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    the error exactly pointing at dao.save(patient)
    Last edited by tokhi; Jun 5th, 2011 at 07:16 AM.

  2. #2
    Join Date
    Apr 2011
    Posts
    107

    Default

    perhaps you forget the @Autowired into your controller (PatientDaoImp dao; => @Autowired PAtientDao dao ?

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    1) There is no @Autowired on the dao in your controller
    2) I see no context:annotation-driven or component-scan in your code, basically rendering your @Autowired useless..
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    May 2010
    Posts
    18

    Default [solved]

    Thanks guys,
    worked great!

  5. #5
    Join Date
    Oct 2011
    Posts
    5

    Default Quick Question

    What would be the difference between @Autowired the DaoImp into the controller and doing SomeDao = SomeDaoImpl?

  6. #6
    Join Date
    Nov 2010
    Location
    Guadalajara, Mexico
    Posts
    11

    Default

    You shoud ask what the difference between using spring or not!. The difference it's in the desing. Doing SomeDao = SomeDAoImpl your actually coupling your application with the SomeDaoImpl and we should avoid doing that (remember that's one of the reasons for using spring, it allows you to loose coupling your app).

  7. #7
    Join Date
    Oct 2011
    Posts
    5

    Question

    Quote Originally Posted by ikim1980 View Post
    You shoud ask what the difference between using spring or not!. The difference it's in the desing. Doing SomeDao = SomeDAoImpl your actually coupling your application with the SomeDaoImpl and we should avoid doing that (remember that's one of the reasons for using spring, it allows you to loose coupling your app).
    Sorry I didn't mean it like that. I understand why using @Autowired decouples our objects (and I totally love the concept), but trying to get that to the head of senior programers (Spring is a new technology @ where I work) has been kind of difficult. They want to create an aspect that can log whenever a change is made to a certain type of object by calling a static method or creating a static class and not having it being autowired. They went ahead and did the change, but they started getting a null pointer exception when calling merge(object), so now I have to make it work somehow, and they hope it can be without @Autowired (sigh )

    I actually tried to create an aspect that would listen to all "execution(* commit()) && args(something,..) || execution(* save(..)) && args(something,..) , but was unable to actually getting it to print out whenever an object is saved to the DB through hibernate. This was more for fun and curiosity

    Thanks for the help btw!

  8. #8
    Join Date
    Nov 2010
    Location
    Guadalajara, Mexico
    Posts
    11

    Default

    Quote Originally Posted by mars009 View Post
    I actually tried to create an aspect that would listen to all "execution(* commit()) && args(something,..) || execution(* save(..)) && args(something,..) , but was unable to actually getting it to print out whenever an object is saved to the DB through hibernate. This was more for fun and curiosity
    sounds you can use hibernate interceptors.

    Best Regards!

  9. #9
    Join Date
    Oct 2011
    Posts
    5

    Default

    Quote Originally Posted by ikim1980 View Post
    sounds you can use hibernate interceptors.

    Best Regards!
    Oh wow, see that's the kind of stuff I don't know about and will love to get my hands on. I'll look into them and we'll see what happens. Thanks for all the help ikim!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •