Announcement Announcement Module
Collapse
No announcement yet.
dao.save(object) - java.lang.NullPointerException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 08:16 AM.

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

    Comment


    • #3
      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..

      Comment


      • #4
        [solved]

        Thanks guys,
        worked great!

        Comment


        • #5
          Quick Question

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

          Comment


          • #6
            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).

            Comment


            • #7
              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!

              Comment


              • #8
                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!

                Comment


                • #9
                  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!!

                  Comment

                  Working...
                  X