• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate exception problem

 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • 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??
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
Rose Rimsky
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
  • 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!!!
 
Tomasz Szymanski
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic