This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes NullPointerException on SessionFactory sessionFactory = new Configuration().configure().buildSession Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "NullPointerException on SessionFactory sessionFactory = new Configuration().configure().buildSession" Watch "NullPointerException on SessionFactory sessionFactory = new Configuration().configure().buildSession" New topic
Author

NullPointerException on SessionFactory sessionFactory = new Configuration().configure().buildSession

Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
Hello,

Following are the files of my application i.e contact.hbm.xml, hibernate.cfg.xml,MyExample.java.
MyExample.java is returning NullPointerException on

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

hibernate.cfg.xml
===========

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>


contact.hbm,xml
===========

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Contact" table="user">
<id name="id" type="long" column="id" >
<generator class="assigned"/>
</id>

<property name="fullname">
<column name="fullname" />
</property>
<property name="address">
<column name="address"/>
</property>
<property name="email">
<column name="email"/>
</property>
</class>
</hibernate-mapping>


MyExample.java
===========

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class MyExample {

public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object

System.out.println("Inserting");
Contact contact = new Contact();
contact.setId(3);
contact.setFullname("Smitha");
contact.setAddress("NY");
contact.setEmail("smithaxxx@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.close();

}

}

}

Could someone please tell what am I missing or what is wrong?

Thanks.


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Print out the stack trace - what else does it say?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
what is the location of hibernate.cfg.xml file. It should be parallel to the classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException on SessionFactory sessionFactory = new Configuration().configure().buildSession
 
Similar Threads
Hibernate--->Sqlserver
Cannot insert record into Database!
session.flush(); gives null
NullPointerException in session.flush()
Problem in Running Hibernate Sample application