本周我开始使用Vaadin,从此给我留下了深刻的印象。但是,我遇到了一个我无法解决的问题。基本上我创建了一个Web应用程序,我在其中显示Contacts(存储在我的数据库中)并将它们显示在表中。
由于我使用的是Hibernate,所以我从以下代码行开始:
JPAContainer<Contact>contactContainer = new JPAContainer<Contact>(Contact.class);
CachingMutableLocalEntityProvider<Contact> entityProvider = new CachingMutableLocalEntityProvider<Contact>(Contact.class, em);
contactContainer.setEntityProvider(entityProvider);
contactsList = new ContactsList(contactContainer);
如您所见,我将容器传递给一个名为ContactsList的类,其中构建了表等:
table = new Table();
table.setWidth(100, Unit.PERCENTAGE);
table.addStyleName(ValoTheme.TABLE_BORDERLESS);
table.addStyleName(ValoTheme.TABLE_COMPACT);
table.setSelectable(true);
table.setColumnCollapsingAllowed(true);
if(container != null)
{
table.setContainerDataSource(this.container);
table.setSortAscending(true);
table.setVisibleColumns("firstName", "lastName", "telephoneNr", "gsmNr", "creationDate");
table.setColumnHeaders(SpringUtil.getMessage("label.contacts_firstname", null),
SpringUtil.getMessage("label.contacts_lastname", null),
SpringUtil.getMessage("label.contacts_telephone_nr", null),
SpringUtil.getMessage("label.contacts_gsm_nr", null),
SpringUtil.getMessage("label.contacts_creationdate", null));
}
table.setMultiSelect(true);
table.setImmediate(true);
当我注释掉所有这些代码时,我的应用程序运行完美而不会崩溃(当然没有显示联系人表)但是当我尝试编译这段代码时,即使在构建了表之后调试一切也很好但是然后是NPE在课程ServerRpcHandler
中抛出:
if (invocation instanceof ServerRpcMethodInvocation) {
try {
ServerRpcManager.applyInvocation(connector,
(ServerRpcMethodInvocation) invocation);
} catch (RpcInvocationException e) {
manager.handleConnectorRelatedException(connector, e);
}
} else {
此处显示完整的StackTrace:
SEVERE: java.lang.NullPointerException
at com.vaadin.addon.jpacontainer.provider.LocalEntityProvider.getEntityManager(LocalEntityProvider.java:215)
at com.vaadin.addon.jpacontainer.provider.LocalEntityProvider.doGetEntityManager(LocalEntityProvider.java:226)
at com.vaadin.addon.jpacontainer.provider.LocalEntityProvider.doGetEntityCount(LocalEntityProvider.java:510)
at com.vaadin.addon.jpacontainer.provider.CachingSupport$FilterCacheEntry.getEntityCount(CachingSupport.java:157)
at com.vaadin.addon.jpacontainer.provider.CachingSupport.getEntityCount(CachingSupport.java:826)
at com.vaadin.addon.jpacontainer.provider.CachingMutableLocalEntityProvider.getEntityCount(CachingMutableLocalEntityProvider.java:130)
at com.vaadin.addon.jpacontainer.JPAContainer.size(JPAContainer.java:912)
at com.vaadin.ui.AbstractSelect.size(AbstractSelect.java:762)
at com.vaadin.ui.Table.refreshRenderedCells(Table.java:1727)
at com.vaadin.ui.Table.attach(Table.java:4298)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:590)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:577)
at com.vaadin.ui.AbstractComponent.setParent(AbstractComponent.java:484)
at com.vaadin.ui.AbstractComponentContainer.addComponent(AbstractComponentContainer.java:210)
at com.vaadin.ui.CssLayout.addComponent(CssLayout.java:120)
at com.vaadin.navigator.Navigator$ComponentContainerViewDisplay.showView(Navigator.java:191)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:568)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:526)
at be.cematech.dashboard.ui.DashboardMenu$ValoMenuItemButton$1.buttonClick(DashboardMenu.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:977)
at com.vaadin.ui.Button.fireClick(Button.java:387)
at com.vaadin.ui.Button$1.click(Button.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:290)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:183)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:92)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1404)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
更新
可能有用的是,我在不同的页面上使用此ContactList,以及我有完全相同的代码行,但它能够显示表格。这是否有两个EntityManagers和/或EntityProviders访问相同的数据?
UPDATE2
@PersistenceContext
protected EntityManager em;
答案 0 :(得分:1)
根据Update2,我看到你在运行时使用注释“@PersistenceContext”检索你的EntityManager。 如果您在“CDI bean,EJB,Servlet,Servlet侦听器,Servlet过滤器或JSF ManagedBean”中使用它,则可以解决此问题。 (http://tomee.apache.org/examples-trunk/injection-of-entitymanager/README.html) 我的猜测是,在“其他页面”上,您处于注释有效的正确上下文中,而在“此”页面中则不是。 检查一下,如果是这种情况,我建议手动实例化EntityManager,例如
JPAContainerFactory.make(Contact.class, "PERSISTENCE_UNIT_NAME");
如果您在项目中使用/使用persistence.xml文件。否则,一种方法是自定义创建像
这样的EntityManagerFactoryPersistence.createEntityManagerFactory( "persistence_unit_name", properties ).createEntityManager()
其中properties是一个持久性属性的地图,如
Map<String, String> properties = new HashMap<String, String>();
properties.put(JDBC_DRIVER, this.jdbcDriver);
properties.put(JDBC_URL, this.url);
properties.put(JDBC_USER, this.username);
properties.put(JDBC_PASSWORD, this.password);
// Configure logging. FINE ensures all SQL is shown
properties.put(LOGGING_LEVEL, "FINE");
return properties;
像这样的EntityManager的好处是,无论何时你在你的应用程序中,你都可以准备好它。 欢呼声。