2

This week I started using Vaadin and has made a big impression on me ever since. However, I came accross a problem which I am not able to resolve. Basically I made a web application where I display Contacts (stored in my database) and display them in a Table.

Since I am using Hibernate, I start off with the following lines of code:

    JPAContainer<Contact>contactContainer = new JPAContainer<Contact>(Contact.class);
    CachingMutableLocalEntityProvider<Contact> entityProvider = new CachingMutableLocalEntityProvider<Contact>(Contact.class, em);
    contactContainer.setEntityProvider(entityProvider);

    contactsList = new ContactsList(contactContainer);

As you can see, I pass my container to a class called ContactsList where the table etc. is built:

    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);

When I comment out all this code, my application runs perfectly without crashing (ofcourse not showing the contacts table) but when I try and compile this code, while debugging everything works well even after the table has been built but then a NPE is thrown in the class ServerRpcHandler on the lines:

if (invocation instanceof ServerRpcMethodInvocation) {
    try {
        ServerRpcManager.applyInvocation(connector,
                 (ServerRpcMethodInvocation) invocation);
        } catch (RpcInvocationException e) {
                  manager.handleConnectorRelatedException(connector, e);
        }
} else {

The full StackTrace is shown here:

    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)

UPDATE

What might be useful to know is that I use this ContactList on a different page as well where I have the EXACT same lines of code but where it is able to show the table. Does this have something to do of having two EntityManagers and/or EntityProviders accessing the same data??

UPDATE2

@PersistenceContext
protected EntityManager em;
RazorAlliance192
  • 722
  • 9
  • 23

1 Answers1

1

As per Update2 I saw you retrive at runtime your EntityManager with the annotation "@PersistenceContext". This works out if you are using it in "CDI bean, EJB, Servlet, Servlet Listener, Servlet Filter, or JSF ManagedBean" ( http://tomee.apache.org/examples-trunk/injection-of-entitymanager/README.html ) My guess is that on the "other page" you are in a right context where the annotation works, while in "this" page you are not. Check this and if it's the case I'd suggest to manually instantiate the EntityManager with for example

JPAContainerFactory.make(Contact.class, "PERSISTENCE_UNIT_NAME");

if you have/use a persistence.xml file in your project. Otherwise a way would be to custom create an EntityManagerFactory like

Persistence.createEntityManagerFactory( "persistence_unit_name", properties ).createEntityManager()

where properties is a Map of persistence properties like

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;

The perk of EntityManager like this is that you can have it ready whenever you are in your app. Cheers.

MarcelloGarini
  • 599
  • 2
  • 13