HI,
I am new for hibernate, while executing main method I am getting nullpointer exception. please some body guide me whats the wrong in my code.
package roseindia.tutorial.hibernate;
import java.io.File;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author Deepak Kumar
*
*
http://www.roseindia.net * Hibernate example to inset data into Contact table
*/
public class FirstExample {
public static void main(
String[] args) {
Session session = null;
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
System.out.println("getEventInfo");
File file = new File("D:\\bea\\user_projects\\w4WP_workspaces\\Untitled\\Hyber\\hibernate.cfg.xml"); //yu see I have used full path
// This is my first run I will chenge to relative path later
System.out.println(file.exists());
SessionFactory sessionFactory = new Configuration().configure(file).buildSessionFactory();
if(sessionFactory == null) {
System.out.println("NULL"); //Testing it
}
else {
System.out.println("NOT NULL");
}
//SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("Sessiong Factory After");
session =sessionFactory.openSession();
//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(3);
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();
}
}
}
Error is
getEventInfo
true
Exception in
thread "Main Thread" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:52)