• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mishaal Khan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print out the stack trace - what else does it say?
 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the location of hibernate.cfg.xml file. It should be parallel to the classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic