This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Can't get the URL or File from the resource of hibernate.cfg.xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Can Watch "Can New topic

Can't get the URL or File from the resource of hibernate.cfg.xml file

Neeraj Khatana

Joined: Jun 08, 2009
Posts: 7

Below is the code given for HibernatePlugIn where the error is coming.

public class HibernatePlugIn implements PlugIn {

private String _configFilePath = "/WEB-INF/classes/hibernate.cfg.xml";

public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();

private SessionFactory _factory = null;

public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
File file = new File(_configFilePath);

Configuration configuration = null;
URL configFileURL = null;
ServletContext context = servlet.getServletContext();

String file_configFilePath = context.getRealPath(_configFilePath);

try {

configFileURL = HibernatePlugIn.class.getResource(file_configFilePath);

configuration = (new Configuration()).configure(configFileURL); // java.lang.NullPointerException occurs in this line(Line no:69)
_factory = configuration.buildSessionFactory();

// Set the factory into session
context.setAttribute(SESSION_FACTORY_KEY, _factory);

} catch (HibernateException e) {
System.out.println("Error while initializing hibernate: "
+ e.getMessage());
}catch(Exception e) {

Below is the exception comes while starting the tomcat. I am using the Netbeans as IDE.
I tried it on tomcat 6.0 and tomcat 5.5. But in both the cases, I got the same error.
I am getting "configFileURL " parameter as null everytime.

at plugin.HibernatePlugIn.init(
at org.apache.struts.action.ActionServlet.initModulePlugIns(
at org.apache.struts.action.ActionServlet.init(
at javax.servlet.GenericServlet.init(
at org.apache.catalina.core.StandardWrapper.loadServlet(
at org.apache.catalina.core.StandardWrapper.load(
at org.apache.catalina.core.StandardContext.loadOnStartup(
at org.apache.catalina.core.StandardContext.start(
at org.apache.catalina.core.ContainerBase.addChildInternal(
at org.apache.catalina.core.ContainerBase.addChild(
at org.apache.catalina.core.StandardHost.addChild(
at org.apache.catalina.startup.HostConfig.deployDescriptor(
at org.apache.catalina.startup.HostConfig.deployDescriptors(
at org.apache.catalina.startup.HostConfig.deployApps(
at org.apache.catalina.startup.HostConfig.start(
at org.apache.catalina.startup.HostConfig.lifecycleEvent(
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(
at org.apache.catalina.core.ContainerBase.start(
at org.apache.catalina.core.StandardHost.start(
at org.apache.catalina.core.ContainerBase.start(
at org.apache.catalina.core.StandardEngine.start(
at org.apache.catalina.core.StandardService.start(
at org.apache.catalina.core.StandardServer.start(
at org.apache.catalina.startup.Catalina.start(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at org.apache.catalina.startup.Bootstrap.start(
at org.apache.catalina.startup.Bootstrap.main(

I am fed up with this error. Everytime, I start the tomcat this error comes and my application does not start.

I don't get the URL or FILE object from the resource of hibernate.cfg.xml file. After that only, i proceed further to get the sessionfactory from this URL or FILE.

Please help me out in this situation.

Thanks and Regards
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

I am a bit confused at why you need this code? If the hibernate.cfg.xml is already in the root of the classpath, which WEB-INF/classes is, and you just want to create a SessionFactory, why don't use just call

SessionFactory sessionFactory = new SessionFactory();

and get rid of all that other code completely. When you call SessionFactory() with the constructor that takes no arguments, it will automatically look at the root of your classpath for a hibernate.cfg.xml file.

Also, I am not sure why you need the factory in the Web layer. Usually you only need it in the middle tier DAO layer, which usually gets jarred up with the Service business layer.

Anyway, just use the no-args constructor to create a SessionFactory and you will be good to go.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Can't get the URL or File from the resource of hibernate.cfg.xml file
It's not a secret anymore!