Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Neeraj Khatana
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic