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"