我有一个调用数据库的 init 方法。这里的想法是,当用户进入应用程序时,init 方法通过以下方式检查用户凭据:
void initMethod() {
/** Some setup stuff **/
hbsession = HibernateUtil.getSessionFactory().getCurrentSession();
buildBean();
hbsession = HibernateUtil.getSessionFactory().getCurrentSession();
user_level = permissionCheck(username);
}
/** Later on in file **/
void login_the_user() {
/****
The problem happens here user_level is null because it seems to execute
faster than the database call.
****/
if (someArray.contains(user_level)) {
/ ** Do stuff **/
}
else { /** Do stuff **/ }
}
它首先填充用户 bean,然后检查用户权限。我遇到的问题是,当我正常运行这个应用程序时,我得到一个 NullPointerException,但如果我在调试中运行它(我在这里放了一个断点)它工作正常。我相信这是因为这些数据库调用在不同的线程上运行需要更多时间,并且在应用程序使用数据时仍然为空。所以我的问题是解决这个问题的最佳方法是什么,以便应用程序能够在实际使用它们之前处理这些方法?
我试过做 Thread.sleep(1000),但这似乎很草率,而且工作了一半。
正常运行时堆栈跟踪如下:
.... Long list of database calls getting all the information ....
Jan 16, 2014 9:43:55 AM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE:
java.lang.NullPointerException
at PatchmgmtUI.permission(PatchmgmtUI.java:331)
at PatchmgmtUI.access$3(PatchmgmtUI.java:328)
at PatchmgmtUI$1.beforeViewChange(PatchmgmtUI.java:154)
at com.vaadin.navigator.Navigator.fireBeforeViewChange(Navigator.java:592)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:550)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:523)
at PatchmgmtUI.loginUser(PatchmgmtUI.java:270)
at PatchmgmtUI.access$0(PatchmgmtUI.java:247)
at PatchmgmtUI$1.beforeViewChange(PatchmgmtUI.java:148)
at com.vaadin.navigator.Navigator.fireBeforeViewChange(Navigator.java:592)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:550)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:523)
at com.vaadin.ui.UI.doInit(UI.java:534)
at com.vaadin.server.AbstractCommunicationManager.getBrowserDetailsUI(AbstractCommunicationManager.java:2468)
at com.vaadin.server.AbstractCommunicationManager.handleBrowserDetailsRequest(AbstractCommunicationManager.java:2362)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:325)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:201)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Jan 16, 2014 9:43:55 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Patchmgmtproject Application] in context with path [/PatchMgmtProject] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at PatchmgmtUI.permission(PatchmgmtUI.java:331)
at PatchmgmtUI.access$3(PatchmgmtUI.java:328)
at PatchmgmtUI$1.beforeViewChange(PatchmgmtUI.java:154)
at com.vaadin.navigator.Navigator.fireBeforeViewChange(Navigator.java:592)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:550)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:523)
at PatchmgmtUI.loginUser(PatchmgmtUI.java:270)
at PatchmgmtUI.access$0(PatchmgmtUI.java:247)
at PatchmgmtUI$1.beforeViewChange(PatchmgmtUI.java:148)
at com.vaadin.navigator.Navigator.fireBeforeViewChange(Navigator.java:592)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:550)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:523)
at com.vaadin.ui.UI.doInit(UI.java:534)
at com.vaadin.server.AbstractCommunicationManager.getBrowserDetailsUI(AbstractCommunicationManager.java:2468)
at com.vaadin.server.AbstractCommunicationManager.handleBrowserDetailsRequest(AbstractCommunicationManager.java:2362)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:325)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:201)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)