I am new to Spring and trying to configure Spring with Hibernate.I am facing problem while getting the this.sessionFactory.getCurrentSession
getting error of null pointer.
configuration in dispatcher-servlet.xml is mentioned.
I tried by putting property name configLocation and value hibernate.cfg.xml but no success.
My DAO implementation is as below:
I am not getting error at the time of start up but when i call listEmp() i get the null pointer exception.
Please help me i will be thankful to you all.
I also noticed you didn't call the list() method, so you are actually trying to return the Query object, which actually should have given you a compile time error.
Also, when you write an HQL query, you use the Objects and property names, so "employee" lower case is probably not the name you gave the class. It probably starts with a capital letter.
Thank you for your reply but still i am facing the issue.So for your reference i have attached the stack trace. I just started to learning the spring and stuck in this situation.I have made the web project in eclipse and trying to make the sample application.
Also i have made changes as you suggested..
Gives me error at sessionFactory.getCurrentSession() but dosent give error while setsessionFactory()
Please give me any suggestion,guide line so i can go ahead with the example.I will be thankful to all of you.
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1077
posted
0
Right, there's your problem. You used "new EmpDaoImpl()" so the object you've created is not managed by Spring. You never called setSessionFactory() and so SessionFactory is of course null, hence an NPE when you try to dereference it.
You probably want to be getting an EmpDao managed by the Spring container which means you need to inject it somehow in the controller. For example you could create a field in your controller and then setup a constructor or setter to inject it.
For example this is a very common pattern in the controllers we write:
When the controller is constructed an EmployerService will be injected by Spring. In other words the EmployerService bean will be managed by Spring, so any injection we've setup in EmployerService will be taken care of as well.
subject: Null Pointer on sessionFactory.getCurrentSession