• 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

Hibernate exception problem

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am learning to use hibernate and studied a few tutorials and starting an example which loads data into oracle database.What i get is an null pointer exception.The same example and the same exception problem has been posted before,but their solution are not working for me.Here is the code that I am working on.
This is hibernate.cfg.xml.I use eclipse for development and this file i have it in the src folder.

This file contact.hbm.xml is also in the src folder where all the java files reside

Contact.java

FirstExample.java


The line where i get the null pointer exception is .Meaning that the session is null.is it connection that is the problem? can someone please help 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
If you are using the latest Hibernate, that approach to coding is no longer needed or valid. You still create a SessionFactory, and create it only once.

Then you just call sessionFactory.getCurrentSession(); No need for a try catch anymore. Also you must have a Transaction, you cannot have a Session without a Transaction. What environment are you running in? Stand-Alone, Tomcat, App Server?

Mark
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Romeo, as Mark already told you, you need to begin a transaction, you cannot flush if you haven't begin one. So be sure to have: session.beginTransaction();

But if the problem continues, please post your complete error trace so we can help you
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Mark and Rose.I am running my application in a standalone environment.I use eclipse.As per your suggestion I have changed my code and I no more get the null pointer exception.I have pasted my changed code below.


But now the problem is that it cannot find the hibernate.cfg.xml which is in my src folder.Both my contact.hbm.xml and hibernate.cfg.xml are in my src folder.The contents that I have in this file is pasted in my previous post.Here is the error that i get

Thanks in advance!!!
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using upper case for the hibernate-configuration, session-factory and mapping tags? Where did that come from? The first thing I would suggest is rewriting them in lower case.
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have changed them into lowercase but that does not solve the problem
 
Rose Rimsky
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Romeo, when you're creating your sessionFactory, try to do it like this:

I think that it will know the location of your hibernate config file, without the need to tell it.

Hope it helps!
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changing the code as per the suggestion above I am getting the these in the stack trace.I am getting null pointer exception



At line 28 I have this
 
Rose Rimsky
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Romeo, I think that's because you're trying to begin a transaction with a session that doesn't have any value. You haven't assigned a value to your session, you just initialized it to null. What you have to do is to obtain the session from session factory. Something like this:

Hope it helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic