• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Weird Exception

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
My log only says this in the Eclipse console :



Thanks and Regards,
Sid.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 66
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Waswani,
Would keep your suggestions in mind. Thanks a ton again !!!.


Thanks and Regards,
Sid.
reply
    Bookmark Topic Watch Topic
  • New Topic