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

Nullpointer exception

 
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 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)
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line does the error occur in? Which object is null, or which method call returns null?

Also, please UseCodeTags when posting ocde of any length.
 
I brought this back from the farm where they grow the tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic