NullPointerException in Jpacontainer 3.1 problems

Hi. I’m trying to display content in a table (using Vaadin 7.1.5) via JPAcontainer 3.1, but unfortunatly, i can’t because i got NullPointerException. I really don’t know why it is happening. I’ve followed the tutorial in the web page of JPAContainer, and different tutorials but in all cases is the same…

I’ve used hibernate and eclipselink and with both of them i have the same result…

The persistence.xml:


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="admin" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <non-jta-data-source>jdbc/admin</non-jta-data-source>
    <class>entities.Proyecto</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
      <property name="eclipselink.logging.level"  value="FINE" />
      <property name="eclipselink.ddl-generation.output-mode" value="database"/>
    </properties>
  </persistence-unit>
</persistence>

The entity class:



@Entity
public class Proyecto extends AbstractEntity {

    private String nombre;
    private String urlBase;

    public Proyecto() {
    }
    public Proyecto(String nombre, String urlBase) {
        this.nombre = nombre;
        this.urlBase = urlBase;
    }
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return super.id;
    }
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public String getUrlBase() {
        return urlBase;
    }
    public void setUrlBase(String urlBase) {
        this.urlBase = urlBase;
    }
}

An here is where i’m calling the table with the JPAContainer:


JPAContainer<Proyecto> cont = JPAContainerFactory.make(Proyecto.class, "admin");
        proysTable = new Table(null, cont);
        proysTable.setImmediate(true);
        proysTable.setVisibleColumns("nombre", "urlBase");
        proysTable.setWidth("100.0%");
        proysTable.setHeight("100.0%");
        proyectosLayout.addComponent(proysTable);

This is the stacktrace …

[b]

SEVERE: java.lang.NullPointerException
at com.vaadin.addon.jpacontainer.provider.LocalEntityProvider.doGetEntityCount(LocalEntityProvider.java:505)
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:1632)
at com.vaadin.ui.Table.attach(Table.java:4147)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.ui.AbstractComponent.setParent(AbstractComponent.java:478)
at com.vaadin.ui.AbstractSingleComponentContainer.setContent(AbstractSingleComponentContainer.java:137)
at com.vaadin.ui.UI.setContent(UI.java:1158)
at Admin.init(Admin.java:33)
at com.vaadin.ui.UI.doInit(UI.java:610)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
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:724)

WARNING: StandardWrapperValve[Admin$Servlet]
: PWC1406: Servlet.service() for servlet Admin$Servlet threw exception
java.lang.NullPointerException
at com.vaadin.addon.jpacontainer.provider.LocalEntityProvider.doGetEntityCount(LocalEntityProvider.java:505)
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:1632)
at com.vaadin.ui.Table.attach(Table.java:4147)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:571)
at com.vaadin.ui.AbstractComponent.setParent(AbstractComponent.java:478)
at com.vaadin.ui.AbstractSingleComponentContainer.setContent(AbstractSingleComponentContainer.java:137)
at com.vaadin.ui.UI.setContent(UI.java:1158)
at Admin.init(Admin.java:33)
at com.vaadin.ui.UI.doInit(UI.java:610)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
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:724)

[/b]
I really hope someone can helpme.

I have the exact same behaivour but I’m using Datanucleus instead of Hibernate, this only occurs with Table or CustomTable,
and seems to be something inside JPAContainer.size() method, whenever it retrieves the entity provider it’s set to Null … I’ve tried with the EntityyManager-per-request pattern as recommended on Vaadin book, but same result

did you find any workaround?

I’m also using Datanucleus as Roberto, and hit the same problem. I’m trying to integrate maven3, vaadin7, JPA2.0 with GAE but stuck in the same place with persistance.

I get rid of NullPointerException however by moving persistence.xml to WEB-INF/classes/META-INF folder, previously it was in WEB-INF, but GAE is giving me the middle finger :frowning: sigh

Any update on this? I’m getting the same problem but with a slightly different stack trace:

Feb 25, 2014 11:46:00 PM com.vaadin.server.DefaultErrorHandler doDefault
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)

This is with Vaadin 7 and JPAContainer 3.1.1…

Hi Noam,

I recently integrated JPAContainer 3.1.1 and Vaadin 7 successfully. Most of my inspiration came from
Book of Vaadin
and Matti (
Using JPA Container 2.0 in a JEE6 server
and
Using JPAContainer in JEE6 server is now much simpler
).

I used Hibernate for persistence with JTA transactions, and had to set up
Vaadin CDI
first. Some errors that translated to NullPointedExceptions consisted of a missing
beans.xml
file or creating a new component that used JPAContainer instead of injecting it.

I remember trying to create the container with JPAContainerFactory to no avail, so I’m posting an example of how I created the container in the end to see if it can help you.

This is my UI:

package com.vaadin.testjpacontainer;

import javax.ejb.EJB;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.cdi.CDIUI;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.entityproviders.MyEntityProvider;
import com.vaadin.entities.MyEntity;

@CDIUI(value="testJpaContainer")
public class TestJPAContainerUI extends UI {
    private static final long serialVersionUID = 1L;

    @EJB
    protected MyEntityProvider myEntityProvider;

    @Override
    protected void init(VaadinRequest request) {
        JPAContainer<MyEntity> container = new JPAContainer<MyEntity>(MyEntity.class);
        unCodeContainer.setEntityProvider(myEntityProvider);
        
        Table table = new Table("Test JPAContainer", container);
        table.setSizeFull();
        
        setContent(table);
        setSizeFull();
    }
}

This is my EntityProvider:

package com.vaadin.testjpacontainer.entityproviders;

import javax.ejb.Stateless;
import com.vaadin.MyEntity;

@Stateless
public class MyEntityProvider extends EjbEntityProvider<MyEntity> {

    public MyEntityProvider() {
        super(MyEntity.class);
    }
}

And its parent:

package com.vaadin.testjpacontainer.entityproviders;

import javax.annotation.PostConstruct;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import com.vaadin.addon.jpacontainer.provider.MutableLocalEntityProvider;

@TransactionManagement
public abstract class EjbEntityProvider<T> extends MutableLocalEntityProvider<T> {
    
    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager em;

    public EjbEntityProvider(Class<T> entityClass) {
        super(entityClass);
    }
   
    @PostConstruct
    public void init() {
        setTransactionsHandledByProvider(false);
        setEntityManager(em);
    }
    
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    protected void runInTransaction(Runnable operation) {
        super.runInTransaction(operation);
    }
}

Regards,
Bernat.

Hi Bernat, thank you for your reply!

The strange thing about this problem is that the addressbook demo works fine right out of the box, and it’s creating the JPAContainer also through the factory (well one of them). I tried to change the way I create the container to be similar to your way, but I’m still seeing the NullPointerException.

It might be a UI issue perhaps, as when I changed my component hierarchy I was able to see my Table. However as soon as I clicked a column header, I got the exception:

Feb 27, 2014 11:41:18 PM com.vaadin.server.DefaultErrorHandler doDefault
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.doPaintContent(Table.java:3211)
    at com.vaadin.ui.Table.paintContent(Table.java:3195)

Here is my init() method right now:

  @Override
  protected void init(VaadinRequest request) {

    // Create a persistent person container
    JPAContainer<Person> persons = JPAContainerFactory.make(Person.class, "sms-gateway");

    // Set up sorting if the natural order is not appropriate
    persons.sort(new String[] { "lastName", "firstName" }, new boolean[]
 { false, false });

    // Bind it to a component
    Table component = new Table("The Persistent People", persons);
    component.setVisibleColumns("id", "lastName", "firstName");
    component.setSizeFull();
    component.setSelectable(true);
    setContent(component);
    setSizeFull();
  }

And my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://java.sun.com/xml/ns/persistence
      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

<persistence-unit name="sms-gateway">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.gateway.persistence.Person</class>

        <properties>
            <property name="javax.persistence.target-database" value="PostgreSQL" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://{postgresUrl}" />
           <property name="javax.persistence.jdbc.user" value="{user}" />
            <property name="javax.persistence.jdbc.password" value="{password}" />
            <property name="javax.persistence.jdbc.ssl" value="true" />
            <property name="eclipselink.logging.level" value="FINE" />
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
        </properties>

</persistence-unit>

</persistence>

Update:

I tried this same JPAContainer test with a new Eclipse Vaadin 7 project. I just added jpacontainer 3.1.1, eclipselink 2.5.1, and the postgresql driver, and the exact code above worked just fine, without throwing exceptions.

This is unfortunate because now I have to try to find out what, in my “real” environment, is causing it to crash. I’m running Vaadin in an Embedded Tomcat container, using Memcached for session persistence.

Maybe the following fact will help someone point me in the right direction:

The stack trace I’m seeing is a NullPointerException caused when LocalEntityProvider tries to dereference “entityProviderManager”, which is NULL. This happens when I do something to the UI, like click on the column header.

This did not happen in my “vanilla” setup, and I can click on the column headers to sort with no problem. However looking at the entityProviderManager it is still NULL. It’s just that in this case for some reason the path through the UI classes is different and it doesn’t need to call the methods (size()?) that would try to de-reference entityManagerProvider. Is this related to the Embedded Tomcat perhaps?

It looks like this is probably related to the session management. In the standalone vanilla setup the Vaadin session is always the same, while in my setup, the Vaadin session changes when I try to interact with the table. Therefore I think JPAContainer doesn’t have context information from the first initialization.