File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate exception problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate exception problem" Watch "Hibernate exception problem" New topic
Author

Hibernate exception problem

Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 95
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??

SCJP 6.0
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rose Rimsky
Greenhorn

Joined: Feb 10, 2010
Posts: 25
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


Fairy tales are more than true: not because they tell us that dragons exist, but because they tell us that dragons can be beaten. – G.K. Chesterton
Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 95
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!!!
Tomasz Szymanski
Greenhorn

Joined: Jan 11, 2010
Posts: 23
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

Joined: Feb 23, 2009
Posts: 95
i have changed them into lowercase but that does not solve the problem
Rose Rimsky
Greenhorn

Joined: Feb 10, 2010
Posts: 25
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

Joined: Feb 23, 2009
Posts: 95
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

Joined: Feb 10, 2010
Posts: 25
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate exception problem