Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException in creating Session Object using Hibernate

 
Agraj Mangal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new to hibernate and was trying my first app with hibernate reading this tutorial: http://www.tutcity.com/view/first-hibernate.6357.html

My hibernate.cfg.xml file looks like


-----------------------------------------------------------------------------------------------------------

My honey.hbm.xml looks like


-----------------------------------------------------------------------------------------------------

My Honey.java looks like



--------------------------------------------------------------------------------------------------

My SessionFactoryUtil.java looks like


--------------------------------------------------------------------------------------------------

Finally my stand alone java program, the client that tests this application "TestExample.java" looks like


---------------------------------------------------------------------------------------------------------

The exception stack trace looks like

Exception in thread "main" java.lang.NullPointerException
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:578)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:516)
at com.mysql.jdbc.PreparedStatement.toString(PreparedStatement.java:3758)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at com.mysql.jdbc.trace.Tracer.printParameters(Tracer.aj:240)
at com.mysql.jdbc.trace.Tracer.printEntering(Tracer.aj:167)
at com.mysql.jdbc.trace.Tracer.entry(Tracer.aj:126)
at com.mysql.jdbc.trace.Tracer.ajc$before$com_mysql_jdbc_trace_Tracer$1$f51c62b8(Tracer.aj:45)
at com.mysql.jdbc.Connection.registerStatement(Connection.java)
at com.mysql.jdbc.Statement.<init>(Statement.java:267)
at com.mysql.jdbc.PreparedStatement.<init>(PreparedStatement.java:435)
at com.mysql.jdbc.ServerPreparedStatement.asSql(ServerPreparedStatement.java:360)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:516)
at com.mysql.jdbc.ServerPreparedStatement.toString(ServerPreparedStatement.java:2433)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at com.mysql.jdbc.trace.Tracer.printParameters(Tracer.aj:240)
at com.mysql.jdbc.trace.Tracer.printEntering(Tracer.aj:167)
at com.mysql.jdbc.trace.Tracer.entry(Tracer.aj:126)
at com.mysql.jdbc.trace.Tracer.ajc$before$com_mysql_jdbc_trace_Tracer$1$f51c62b8(Tracer.aj:45)
at com.mysql.jdbc.Connection.registerStatement(Connection.java)
at com.mysql.jdbc.Statement.<init>(Statement.java:267)
at com.mysql.jdbc.PreparedStatement.<init>(PreparedStatement.java:417)
at com.mysql.jdbc.ServerPreparedStatement.<init>(ServerPreparedStatement.java:290)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4656)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4573)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.prepareSelectStatement(AbstractBatcher.java:145)
at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:107)
at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:68)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy0.save(Unknown Source)
at com.TestExample.createHoney(TestExample.java:118)
at com.TestExample.main(TestExample.java:19)

--------------------------------------------------------------------------------------------------------------------------------------------------


I'm sorry to have written all my code since i do not know because of which settings the error is coming.
I read a similar problem post on this forum : http://www.coderanch.com/t/216619/Object-Relational-Mapping/java/HI-got-null-pointer-exception
but the solution mentioned there did not helped me.

Is there a problem in hibernate.cfg.xml file ?
Why is my session null ??
because as far as i can understand it is throwing NullPointerException when i try and call any function on session object
like it is giving error in
session.save(honey)
in
createHoney(honey) method

Please help and reply what could be the problem

Best Regards,
Agraj
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic