wood burning stoves 2.0*
The moose likes Spring and the fly likes Null Pointer on sessionFactory.getCurrentSession Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Null Pointer on sessionFactory.getCurrentSession" Watch "Null Pointer on sessionFactory.getCurrentSession" New topic
Author

Null Pointer on sessionFactory.getCurrentSession

Jordan Josh
Ranch Hand

Joined: Aug 23, 2008
Posts: 36
Hi All,

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.

Regards
Jatan
Thank you all For helping me
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

So I assume your NPE happens in this line

return (List<Employee>)this.sessionFactory.getCurrentSession().createQuery("from employee");

Try to break it into a couple of lines instead to find out what is actually giving you the NPE.

like

Session session = sessionFactory.getCurrentSession();
return session.createQuery("from Employee").list();

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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jordan Josh
Ranch Hand

Joined: Aug 23, 2008
Posts: 36
Hi Mark,

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()

EmpDaoImpl is:




My controller is as follow:


My dispatcher-servlet.xml is:



employee.hbm file is


I have used the following Jar file..

antlr-2.7.6.jar
antlr-runtime-3.0.jar
asm-1.5.3.jar
cglib-2.1_3.jar
classes12.jar
commons-collections-2.1.1.jar
commons-dbcp.jar
commons-logging-1.0.4.jar
commons-pool-1.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate-3.1.2.jar
hibernate3.3.1-javassist-3.4.GA.jar
javax.el.jar
jstl.jar
jta.jar
ojdbc14.jar
org.springframework.asm-3.0.4.RELEASE.jar
org.springframework.beans-3.0.4.RELEASE.jar
org.springframework.context-3.0.4.RELEASE.jar
org.springframework.context.support-3.0.4.RELEASE.jar
org.springframework.core-3.0.4.RELEASE.jar
org.springframework.expression-3.0.4.RELEASE.jar
org.springframework.jdbc-3.0.4.RELEASE.jar
org.springframework.orm-3.0.4.RELEASE.jar
org.springframework.transaction-3.0.4.RELEASE.jar
org.springframework.web-3.0.4.RELEASE.jar
org.springframework.web.servlet-3.0.4.RELEASE.jar
spring-webmvc-2.5.6.jar
spring-webmvc-3.0.0.RELEASE.jar


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: 1078


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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer on sessionFactory.getCurrentSession