Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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.


 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print out the stack trace - what else does it say?
 
Ranch Hand
Posts: 147
  • 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.
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic