File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Can Watch "Can New topic
Author

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

Neeraj Khatana
Greenhorn

Joined: Jun 08, 2009
Posts: 7
Hi,

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());
e.printStackTrace(System.out);
}catch(Exception e) {
e.printStackTrace(System.out);
}
}

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.

java.lang.NullPointerException
at plugin.HibernatePlugIn.init(HibernatePlugIn.java:69)
at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:839)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:332)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4045)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4351)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)



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
Neeraj
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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.

Mark
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't get the URL or File from the resource of hibernate.cfg.xml file
 
Similar Threads
Head First page 740 getting error in log file
getting error while loading action servlet for eg in the book Head First Servlet
ClassNotFoundException while running jdbc program
Can't get the URL or File from the resource of hibernate.cfg.xml file
Running in tomcat5 but not in tomcat 6