• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Null Pointer on sessionFactory.getCurrentSession

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic