aspose file tools
The moose likes Object Relational Mapping and the fly likes Nullpointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Nullpointer exception" Watch "Nullpointer exception" New topic
Author

Nullpointer exception

Bindu Sanju
Greenhorn

Joined: Feb 23, 2006
Posts: 16
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("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();

}

}

}

Error is

getEventInfo
true
Exception in thread "Main Thread" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:52)
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Nullpointer exception