1

我正在使用 gwt 和 hibernate 创建一个应用程序。这里我使用下面给出的代码来构建会话工厂:

  Configuration configuration = new Configuration();
    System.out.println("configuration: " + configuration);
    Configuration configur = configuration.configure("com/aro/hibernate.cfg.xml");
    java.util.Properties prop = configur.getProperties();
    System.out.println("prop "+prop);           
    System.out.println("configur: " + configur);
    SessionFactory sessFactory = configur.buildSessionFactory();

这里 sessFactory 在最后一行执行时抛出 NullPointerException 。异常堆栈跟踪如下:

Initializing App Engine server
Apr 30, 2012 12:43:43 PM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger
Apr 30, 2012 12:43:44 PM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
INFO: Successfully processed E:\workSpace\abc2\war\WEB-INF/appengine-web.xml
Apr 30, 2012 12:43:44 PM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
WARNING: appengine-web.xml does not contain a <threadsafe> element. This will become required in a future release.
See http://code.google.com/appengine/docs/java/config/appconfig.html#Using_Concurrent_Requests for more information.
You probably want to enable concurrent requests.
Apr 30, 2012 12:43:44 PM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
INFO: Successfully processed E:\workSpace\abc2\war\WEB-INF/web.xml
Apr 30, 2012 6:13:45 PM com.google.appengine.tools.development.DevAppServerImpl start
INFO: The server is running at http://localhost:8888/
Apr 30, 2012 6:13:46 PM com.google.appengine.tools.development.DevAppServerImpl start
INFO: The admin console is running at http://localhost:8888/_ah/admin
40 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.Environment - Hibernate 3.3.1.GA
46 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.Environment - hibernate.properties not found
53 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
62 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
configuration: org.hibernate.cfg.Configuration@11486aa
190 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
190 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
340 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/aro/client/Abc2Bean.hbm.xml
450 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.HbmBinder - Mapping class: com.aro.client.Abc2Bean -> signup
478 [23163381@qtp-26726104-0] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
prop {hibernate.connection.password=arosys123, java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=C:\Program Files\Java\jre6\bin, java.vm.version=19.0-b09, hibernate.connection.username=root, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, java.util.logging.config.file=WEB-INF/logging.properties, user.country=US, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=, com.google.appengine.application.id=no_app_id, java.vm.specification.name=Java Virtual Machine Specification, user.dir=E:\workSpace\abc2\war, java.runtime.version=1.6.0_23-b05, org.mortbay.log.class=com.google.apphosting.utils.jetty.JettyLogger, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\Program Files\Java\jre6\lib\endorsed, os.arch=x86, java.io.tmpdir=C:\Users\rahul\AppData\Local\Temp\, line.separator=
, java.vm.specification.vendor=Sun Microsystems Inc., user.variant=, os.name=Windows 7, devappserver-thread-23163381@qtp-26726104-0=true, prospectivesearch.autocommit=true, sun.jnu.encoding=Cp1252, java.library.path=C:\Program Files\Java\jre6\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;E:\eclipse;, com.google.appengine.runtime.version=1.6.4, java.specification.name=Java Platform API Specification, java.class.version=50.0, sun.management.compiler=HotSpot Client Compiler, os.version=6.1, datastore.default_high_rep_job_policy_unapplied_job_pct=50, com.google.appengine.application.version=1.1, user.home=C:\Users\rahul, java.security.policy==C:\Users\rahul\AppData\Local\Temp\test8925387590881978776.policy, user.timezone=Asia/Calcutta, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.6, hibernate.connection.driver_class=com.mysql.jdbc.Driver, com.google.appengine.runtime.environment=Development, java.class.path=E:\workSpace\abc2\src;E:\workSpace\abc2\test-classes;E:\workSpace\abc2\war\WEB-INF\classes;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\appengine-local-runtime-shared.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\el-api.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\jsp\repackaged-appengine-ant-1.7.1.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\jsp\repackaged-appengine-ant-launcher-1.7.1.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\jsp\repackaged-appengine-jasper-6.0.29.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\jsp\repackaged-appengine-jasper-el-6.0.29.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\jsp\repackaged-appengine-tomcat-juli-6.0.29.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\jsp-api.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\shared\servlet-api.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\appengine-api-1.0-sdk-1.6.4.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\appengine-api-labs-1.6.4.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\appengine-jsr107cache-1.6.4.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\jsr107cache-1.1.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\orm\datanucleus-appengine-1.0.10.final.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\orm\datanucleus-core-1.1.5.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\orm\datanucleus-jpa-1.1.5.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\orm\geronimo-jpa_3.0_spec-1.1.1.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\orm\geronimo-jta_1.1_spec-1.1.1.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\user\orm\jdo2-api-2.3-eb.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\appengine-tools-api.jar;E:\eclipse\plugins\com.google.gwt.eclipse.sdkbundle_2.4.0.v201203300216-rel-r36\gwt-2.4.0\gwt-user.jar;E:\eclipse\plugins\com.google.gwt.eclipse.sdkbundle_2.4.0.v201203300216-rel-r36\gwt-2.4.0\gwt-dev.jar;E:\eclipse\plugins\com.google.gwt.eclipse.sdkbundle_2.4.0.v201203300216-rel-r36\gwt-2.4.0\validation-api-1.0.0.GA-sources.jar;E:\eclipse\plugins\com.google.gwt.eclipse.sdkbundle_2.4.0.v201203300216-rel-r36\gwt-2.4.0\validation-api-1.0.0.GA.jar;C:\Users\rahul\Desktop\projectJar\antlr-2.7.6.jar;C:\Users\rahul\Desktop\projectJar\commons-collections-2.1.1.jar;C:\Users\rahul\Desktop\projectJar\dom4j-1.6.1.jar;C:\Users\rahul\Desktop\projectJar\jta.jar;C:\Users\rahul\Desktop\projectJar\mysql-connector-java-5.1.7-bin.jar;C:\apache-tomcat-7.0.27\lib\annotations-api.jar;C:\apache-tomcat-7.0.27\lib\catalina-ant.jar;C:\apache-tomcat-7.0.27\lib\catalina-ha.jar;C:\apache-tomcat-7.0.27\lib\catalina-tribes.jar;C:\apache-tomcat-7.0.27\lib\catalina.jar;C:\apache-tomcat-7.0.27\lib\ecj-3.7.2.jar;C:\apache-tomcat-7.0.27\lib\el-api.jar;C:\apache-tomcat-7.0.27\lib\jasper-el.jar;C:\apache-tomcat-7.0.27\lib\jasper.jar;C:\apache-tomcat-7.0.27\lib\jsp-api.jar;C:\apache-tomcat-7.0.27\lib\servlet-api.jar;C:\apache-tomcat-7.0.27\lib\tomcat-api.jar;C:\apache-tomcat-7.0.27\lib\tomcat-coyote.jar;C:\apache-tomcat-7.0.27\lib\tomcat-dbcp.jar;C:\apache-tomcat-7.0.27\lib\tomcat-i18n-es.jar;C:\apache-tomcat-7.0.27\lib\tomcat-i18n-fr.jar;C:\apache-tomcat-7.0.27\lib\tomcat-i18n-ja.jar;C:\apache-tomcat-7.0.27\lib\tomcat-jdbc.jar;C:\apache-tomcat-7.0.27\lib\tomcat-util.jar;E:\workSpace\abc2\war\WEB-INF\lib\antlr-2.7.6.jar;E:\workSpace\abc2\war\WEB-INF\lib\appengine-api-1.0-sdk-1.6.4.jar;E:\workSpace\abc2\war\WEB-INF\lib\appengine-api-labs-1.6.4.jar;E:\workSpace\abc2\war\WEB-INF\lib\appengine-jsr107cache-1.6.4.jar;E:\workSpace\abc2\war\WEB-INF\lib\asm.jar;E:\workSpace\abc2\war\WEB-INF\lib\commons-collections-2.1.1.jar;E:\workSpace\abc2\war\WEB-INF\lib\datanucleus-appengine-1.0.10.final.jar;E:\workSpace\abc2\war\WEB-INF\lib\datanucleus-core-1.1.5.jar;E:\workSpace\abc2\war\WEB-INF\lib\datanucleus-jpa-1.1.5.jar;E:\workSpace\abc2\war\WEB-INF\lib\dom4j-1.6.1.jar;E:\workSpace\abc2\war\WEB-INF\lib\geronimo-jpa_3.0_spec-1.1.1.jar;E:\workSpace\abc2\war\WEB-INF\lib\geronimo-jta_1.1_spec-1.1.1.jar;E:\workSpace\abc2\war\WEB-INF\lib\gwt-servlet.jar;E:\workSpace\abc2\war\WEB-INF\lib\hibernate-core-3.3.1.GA.jar;E:\workSpace\abc2\war\WEB-INF\lib\javassist-3.9.0.GA.jar;E:\workSpace\abc2\war\WEB-INF\lib\jdo2-api-2.3-eb.jar;E:\workSpace\abc2\war\WEB-INF\lib\jsr107cache-1.1.jar;E:\workSpace\abc2\war\WEB-INF\lib\jta.jar;E:\workSpace\abc2\war\WEB-INF\lib\mysql-connector-java-5.1.7-bin.jar;E:\workSpace\abc2\war\WEB-INF\lib\slf4j-api-1.6.1-tests.jar;E:\workSpace\abc2\war\WEB-INF\lib\slf4j-simple-1.0.1.jar;E:\workSpace\abc2\war\WEB-INF\lib\slf4j.api-1.6.1.jar;C:\Users\rahul\Desktop\projectJar\javassist-3.9.0.GA.jar;C:\Users\rahul\Desktop\projectJar\asm.jar;C:\Users\rahul\Desktop\jarnew\ehcache-1.2.3.jar;C:\Users\rahul\Desktop\jarnew\ejb3-persistence.jar;C:\Users\rahul\Desktop\jarnew\hibernate3.jar;C:\Users\rahul\Desktop\jarnew\hibernate-tools.jar;C:\Users\rahul\Desktop\jarnew\jdbc2_0-stdext.jar;C:\Users\rahul\Desktop\jarnew\hibernate-annotations.jar;C:\Users\rahul\Desktop\jarnew\hibernate-commons-annotations.jar;C:\Users\rahul\Desktop\jarnew\hibernate-entitymanager.jar;C:\Users\rahul\Desktop\jarnew\commons-logging-1.1.jar;C:\Users\rahul\Desktop\jarnew\asm-attrs.jar;C:\Users\rahul\Desktop\jarnew\cglib-2.1.3.jar;C:\Users\rahul\Desktop\jarnew\commons-io-1.2.jar;E:\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.4.v201203300216r36\appengine-java-sdk-1.6.4\lib\agent\appengine-agent.jar, user.name=rahul, hibernate.bytecode.use_reflection_optimizer=false, java.vm.specification.version=1.0, java.home=C:\Program Files\Java\jre6, sun.arch.data.model=32, hibernate.connection.url=jdbc:mysql://localhost:3306/test, hibernate.dialect=org.hibernate.dialect.MySQLDialect, user.language=en, java.specification.vendor=Sun Microsystems Inc., awt.toolkit=sun.awt.windows.WToolkit, java.vm.info=mixed mode, sharing, java.version=1.6.0_23, java.ext.dirs=C:\Program Files\Java\jre6\lib\ext;C:\Windows\Sun\Java\lib\ext, sun.boot.class.path=C:\Program Files\Java\jre6\lib\resources.jar;C:\Program Files\Java\jre6\lib\rt.jar;C:\Program Files\Java\jre6\lib\sunrsasign.jar;C:\Program Files\Java\jre6\lib\jsse.jar;C:\Program Files\Java\jre6\lib\jce.jar;C:\Program Files\Java\jre6\lib\charsets.jar;C:\Program Files\Java\jre6\lib\modules\jdk.boot.jar;C:\Program Files\Java\jre6\classes, java.vendor=Sun Microsystems Inc., file.separator=\, hibernate.hbm2ddl.auto=update, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86}
configur: org.hibernate.cfg.Configuration@11486aa
Apr 30, 2012 6:13:59 PM com.google.appengine.tools.development.ApiProxyLocalImpl log
SEVERE: javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.lang.String com.aro.client.GreetingService.greetServer(java.lang.String)' threw an unexpected exception: java.lang.NullPointerException
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:60)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:78)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:369)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.NullPointerException
    at com.aro.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:112)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
    ... 34 more

那么我该如何解决这个问题。谢谢拉胡尔

4

1 回答 1

1

尝试这样做:

   private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();

        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

并且您在 src 文件夹中的 hibernate.cfg.xml 应该是:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tutorialDB</property>
    <property name="hibernate.connection.username">dummyuser</property>
    <property name="hibernate.connection.password">dummyuser</property>
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <mapping class="com.vanilla.entity.Student" />
</session-factory>
</hibernate-configuration>
于 2012-04-30T10:16:21.087 回答