• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer exception using session factory

 
Bindu Sanju
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
 
Bindu Sanju
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example from roseindia.net.

Donno why the sessionFactory object is not gettng formed.
 
Waswani Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Waswani Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through this post. You will understand the reason.

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

Regards,
Naresh Waswani
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic