0

我的所有 Microsoft sql 数据库连接都运行良好,hql 编辑器通过返回结果证明了这一点,但是当我运行应用程序时,我得到了这个错误,它指向一个空指针异常:

SEVERE: Initial SessionFactory creation failed.java.lang.NullPointerException
WARNING: StandardWrapperValve[action]: PWC1406: Servlet.service() for servlet action threw 
exception
java.lang.ExceptionInInitializerError
at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:34)
at hibernate.TimeSheetHelper.<init>(TimeSheetHelper.java:25)
at com.myapp.struts.LoginAction.populateTimeSheet(LoginAction.java:120)
at com.myapp.struts.LoginAction.execute(LoginAction.java:67)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call
(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.Configuration.configure(Configuration.java:1441)
at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:29)
... 33 more

这是我的 HibernateUtil 文件:

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml) 
// config file.
URL myUrl = Thread.currentThread().getContextClassLoader().getResource  
("C:\\java\\hibernate\\hibernate.cfg.xml");
Configuration c = new Configuration();
c.configure(myUrl);
sessionFactory = c.buildSessionFactory();
} catch (Throwable ex) {
// Log the exception. 
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
4

2 回答 2

0

ClassLoader.getResource()不将文件路径作为参数。它采用从类路径加载的资源的路径。因此,您的文件必须位于应用程序类路径中的其中一个 jar(或目录)中,如果它在 packagefoo.bar中,则路径应该是foo/bar/hibernate.cfg.xml.

于 2013-01-25T09:28:54.127 回答
0

我创建了 Session 对象并将其分配为 null。

删除 Null 使其工作。

于 2013-01-25T10:01:13.430 回答