Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer on sessionFactory.getCurrentSession

 
Jordan Josh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jordan Josh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic