wood burning stoves*
The moose likes Object Relational Mapping and the fly likes null pointer exception using session factory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "null pointer exception using session factory" Watch "null pointer exception using session factory" New topic
Author

null pointer exception using session factory

Bindu Sanju
Greenhorn

Joined: Feb 23, 2006
Posts: 16
HI I got the exception while using this below code.

// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();

Configuration cfg = new Configuration();
cfg.addFile("/bin/hibernate.cfg.xml");
SessionFactory sesfact=(SessionFactory) cfg.configure().buildSessionFactory();
session=sesfact.openSession();

System.out.println("session : "+session);
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();


java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:45)
Exception in thread "main"
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What are you doing on line 45? That is where the NullPointerException is being thrown - without line numbers it is difficult for us to help.

That aside, why are you trying to create a SessionFactory twice?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();



Remove those lines, you can't open a session if there is no configuration. Just like what Paul is saying.

Mark


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

Joined: Feb 23, 2006
Posts: 16
HI Poul/Mark,

THanks for your reply, I got stuck up this below code, I am getting null pointer exception in this level. in this folder *.cfg.xml file also available.but still its throwing null pointer exception.


// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Ashoka Jayasekara
Greenhorn

Joined: Feb 01, 2011
Posts: 12
Is this the example in Roseindia.com? i also got the same Exception when i tried to run it. it would be grateful if someone can explain this.
Vaibhav Ranglani
Greenhorn

Joined: Sep 23, 2010
Posts: 1
This is an example from roseindia.net.

Donno why the sessionFactory object is not gettng formed.
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hello,

It looks like some jars are missing in your application. Wrap your code in a try/catch block and catch the Throwable object some thing like below.

try
{
/// code
/// code
}
catch(Throwable t)
{
t.printStackTrace();
}

You should get some exception on which class is missing.

Regards,
Naresh Waswani


Naresh Waswani
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Waswani Naresh wrote:It looks like some jars are missing in your application. Wrap your code in a try/catch block and catch the Throwable object some thing like below.

Why do you think that? In not seeing any ClassNotFoundExceptions.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Please go through this post. You will understand the reason.

http://www.coderanch.com/t/540481/ORM/java/Weird-Exception

Regards,
Naresh Waswani
 
jQuery in Action, 2nd edition
 
subject: null pointer exception using session factory
 
Similar Threads
Building Session Factory - Null pointer Exception
data insert error
session.flush()
HI I got the null pointer exception using session factory
hibernate sample done but no record inserted