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 Weird Exception 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 "Weird Exception" Watch "Weird Exception" New topic
Author

Weird Exception

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi Friends,
I am facing a weird exception while running a simple program in Hibernate.

Following is the code snippet.



hibernarte.cfg.xml



When I run this program in eclipse I get the following exception.



It shows NullPointer at session.flush

It seems that the session factory is not getting created properly.

What is the problem with the code. Why is sessionfactory not getting created properly. Please help !!!

Thanks and Regards,
Sid.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If the SessionFactory fails in its configuration step you will be able to see why in the stack trace Hibernate will produce. What else does your log say?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi Paul,
My log only says this in the Eclipse console :



Thanks and Regards,
Sid.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate reports errors if there are any in its configuration, however you can write code that poorly displays these errors. Why not print the stack trace rather than the message? This code:

only works when an exception contains a message.
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi Paul,
I wrote the line for printing stack trace - e.printStacktrace() - but to no avail. Its still showing the following line in the console.




Thanks and Regards,
Sid.
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hi Sid,

Nothing is wrong with your configuration and/or the main code.

The issue is --> you do not have few jars in your class path that are internally referred by Hibernate framework and because of which when you try to execute the program, you get a java.lang.NoClassDefFoundError and because you have a catch() block looking for Exceptions, it is not able to catch this error.

Replace Exception with Throwable in catch() block and you will come to know which class/classes are missing.


Regards,
Naresh Waswani

Naresh Waswani
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi Naresh,
Thanks for your post. It worked. Should I have both catch(Throwable e) and catch(Exception e) so that I know about classes which are missing as well as the exceptions ? Can we do this as a good practice ?

Thanks and Regards,
Sid.
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hi Sid,

Since Throwable is the parent class of Exception, it can catch any type of exceptions or errors. Generally its a good practice to have generic Exception at the end but if you think there could be some error sort of scenarios, Exception can be replaced with Throwable class.

Regards,
Naresh Waswani
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi Waswani,
Would keep your suggestions in mind. Thanks a ton again !!!.


Thanks and Regards,
Sid.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Weird Exception
 
Similar Threads
NullPointerException on SessionFactory sessionFactory = new Configuration().configure().buildSession
Cannot insert record into Database!
Beginner in hibernate getting error : org.hibernate.MappingException: Unknown entity
NullPointerException in hibernate
Doubt in using JNDI with Hibernate.