• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

null pointer exception using session factory

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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("[email protected]");
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"
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
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

// 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an example from roseindia.net.

Donno why the sessionFactory object is not gettng formed.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please go through this post. You will understand the reason.

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

Regards,
Naresh Waswani
reply
    Bookmark Topic Watch Topic
  • New Topic