• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sping+hibernate got NullException

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,I got a problem in my application which uses spring and hibernate
the code is as follows:

List list=this.getHibernateTemplate().find( "from AccdentsVO o where o.accidentsID=?", new Integer(1));
I got a "java.lang.NullException" in the above line.
The AccidentsVO class is a javabean with a couple of getter and setter methods for examle:setAccidentsID(int i);int getAccidentsID() Furthermore I am sure there is a record in my accident table whose accidentsID is 1.It does work when I use getHibernateTemplate().save(...) I am not very clear why this time I can not use the find(...) method in spring framework.
Regards
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by YanJun Tong:
List list=this.getHibernateTemplate().find( "from AccdentsVO o where o.accidentsID=?", new Integer(1));


Did the above line cause the NullPointerException (first line in stack trace)? If so, getHibernateTemplate() must be returning null for this cannot be null. If not, you'll need to include the actual stack trace for anyone to be able to help you.
 
YanJun Tong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi it seems like the this.getHibernateTemplate().find(...)causes the exception. Do you mean that the getHibernateTemplate()returns null? But in the same class I have another method called save(...)which looks like this.getHibernateTemplate().save(accident); and it works(I mean no exception is raised. )
This makes me confused.
 
YanJun Tong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the getHibernateTemplate() and found that it is null.My test is as follows:
if(getHibernateTemplate()!=null)
{
system.out.println("not null");
}
Unfortunately I got nothing, which means getHibernateTemplate()==null.
Could anybody tell under which condition the getHibernateTemplate()method will return null? thanks
[ September 30, 2004: Message edited by: YanJun Tong ]
 
YanJun Tong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could anybody help me?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi YanJun Tong
I also got the same error once.
i got that error because i tried to call the DAO directly by creating the object.
(ie:i tried to call a method named item.save from DAO.i think yours is the same problem)
.This is not the currect method.
for rectifying this
you have to create a bean for u r DAO in servlet.xml that should refence the "session factory".
create a property in u r controller bean and reference the DAO bean.
remember that a getter and setter method that supports the DAO should be there in u r controller class.(else it will show exception)
then in the controller class u can call u r DAO methods.

message me after trying this.
if u r not getting again i can send u some shorted codes for the above
 
YanJun Tong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sanu I found my problem. You are right that I do need to create a bean for my DAO in servlet.xml Thanks for that
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a problem in same set up i.e. Spring-Hibernate for MySQL. I add my record from a web page and keep the dateAdded field value null. Because in my database dateAdded field map to timestamp. So when a null value is passed MySQL will give it a current time stamp. I can successfully add a record in the database by using

But when I retrieve the object in the same method call i.e. after saving the record in the database I want to retrieve it back to display. The value of dateAdded remains null which causes NullPointerException during date formatting.

To me it looks like a committ problem, that it has not done a committ, and it is retreiving the object in a same connection.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lahori Munda:
But when I retrieve the object in the same method call i.e. after saving the record in the database I want to retrieve it back to display. The value of dateAdded remains null which causes NullPointerException during date formatting.

When you save an object, it doesn't read it back from the database. As well, it's now in the Session (unless you clear() it or open a new Session) and will be returned when read in the future. Finally, if you are using a second-level inter-Session cache, you'll read the old object without the timestamp from there instead of the database.

If you need to refresh the object with the database, which you need to do to pick up the database-generated timestamp field, you can use (IIRC, check the JavaDocs)I'm new to Hibernate myself, so I don't know off the top of my head whether or not that bypasses the second level cache. I recall reading that it does, however.

Another option (I think refresh() does this) is to acquire a read lock on the object using lock(object, lockType).
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to Spring and Hibernate and am having a similar problem. I keep getting a java.lang.NullPointerException when I try to access this: from within my DAO object which is called from the Spring service implementation by: and that is called by the form processing method by: Am I missing something obvious? Please help.

- Paul

[ December 01, 2004: Message edited by: Paul Jr. Durcek ]
[ December 01, 2004: Message edited by: Paul Jr. Durcek ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks sanu.... you saved me a lot of debugging...
 
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic