aspose file tools*
The moose likes Object Relational Mapping and the fly likes NullPointerException when run stand alone program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "NullPointerException when run stand alone program" Watch "NullPointerException when run stand alone program" New topic
Author

NullPointerException when run stand alone program

Harathi Rao
Ranch Hand

Joined: Oct 31, 2004
Posts: 42
Hi,
I am new to hibernate. I am trying to run a standalone program on hibernate3 using mySql database. I am getting the following exception

Exception in thread "main" java.lang.NullPointerException
at org.hibernate.jdbc.AbstractBatcher.closeConnection(AbstractBatcher.ja
va:505)
at org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(Transac
tionHelper.java:142)
at org.hibernate.id.TableGenerator.generate(TableGenerator.java:94)
at org.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:
61)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId
(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGene
ratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrR
equestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTran
sient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(
DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpda
te(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:524)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:510)
at Test.main(Test.java:17)

My hibernate config file is as follows...

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

<hibernate-configuration>
<session-factory>
<property name="connection.driver">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/products</property>
<property name="hibernate.connection.username"></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>
<mapping resource="CD.hbm.xml" />
</session-factory>
</hibernate-configuration>

And my mapping file is

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

<hibernate-mapping>
<class name="CD" table="cd">
<id name="id" type="int" unsaved-value="null">
<column name="ID" sql-type="int" not-null="true"/>
<generator class="hilo"/>
</id>
<property name="title"/>
<property name="artist"/>
<property name="purchasedate" type="date"/>
<property name="cost" type="double"/>
</class>
</hibernate-mapping>

And also I am using jdbc driver "mysql-connector-java-3.0.16-ga-bin.jar".
Can anyone guide me to break through this.

Thanks
Harathi Rao
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

It is on this line

Test.main(Test.java:17)

So, we can't see your code, but one of the objects you are using on this line is set to null.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Harathi Rao
Ranch Hand

Joined: Oct 31, 2004
Posts: 42
Test.java is as follows..

public class Test
{

public static void main(String args[])
{
System.out.print("111*******************");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
System.out.print("222*******************");
CD cd = new CD("Balayya", "AllariPidugu", new Date(), 5);
System.out.print("333*******************\n"+cd.getTitle()+"\n");
session.save(cd); //Exceptions is coming here....
System.out.print("444*******************\n");
session.flush();
session.close();
}
}

At Test.java(line 17), the statement is
session.save(cd);
There is no null object here. As cd.getTitle() is being printed.

CD.java is below..

public class CD
{
int id;
String title;
String artist;
Date purchasedate;
double cost;

public CD(String title, String artist, Date purchasedate, double cost)
{
// constructor
}
// setter and getter for all the fields
}

Please guide me for this....

thanks
harathi rao
Harathi Rao
Ranch Hand

Joined: Oct 31, 2004
Posts: 42
I got rid of the problem finally. The problem is due to hibernate.cfg.xml. I put <property name="connection.driver">...</property>
<property name="connection.url">.........</property>

instead of
<property name="hibernate.connection.driver_class">...</property>
<property name="hibernate.connection.url">.........</property>


Hibernate is not giving the error exactly. So, we need to make sure that config file is well formatted.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Good Job finding that.

Mark
Preetish Madalia
Ranch Hand

Joined: Dec 12, 2007
Posts: 37
Hi All,
I am getting the same problem today. I have checked my hibernate.cfg.xml. All the properties are correct but still the same error is coming. Can any one suggest any new solution for this problem. Thanks in advance.

<?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.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:microsoft:sqlserver://localhost/HYDHTC27338:1433</property>
<property name="hibernate.connection.username"></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.SQLServerDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="com/developerworks/ajax/hibernate/pojo/Items.hbm.xml"/>
</session-factory>
</hibernate-configuration>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException when run stand alone program