2 Replies Latest reply on Jan 3, 2009 4:51 AM by Tristan Stevens

    Can't inject PersistenceContext, NullPointerException when u

    Tristan Stevens Newbie

      I have been working through a JBoss (5.0.0.CR2) example using the tutorial at http://www.netbeans.org/kb/55/ejb30-jboss.html#Exercise_5 (however using Netbeans 6.5).

      I have an entity set up and upon deployment it is creating the table on the database. My problem is when I try to use a SLSB as a facade to retrieve entities back from the database. The generated code is as follows:

      public class CustomerFacade implements CustomerFacadeLocal {
       private EntityManager em;
       public void create(Customer customer) {
       public void edit(Customer customer) {
       public void remove(Customer customer) {
       public Customer find(Object id) {
       return em.find(Customer.class, id);
       public List<Customer> findAll() {
       return em.createQuery("select object(o) from Customer as o").getResultList();

      Unfortunately when I run this code, I get a NullPointerException upon usage of em because the PersistenceContext isn't being injected (I presume). This I'm sure is a config error, however I have also tried
      @PersistenceUnit(unitName = "PrototypeCRMSystem-ejbPU")
       private EntityManagerFactory emf;
       private EntityManager em = emf.createEntityManager();
      which gives the same problem when accessing emf.

      My persitence.xml is as follows:

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="1.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_1_0.xsd">
       <persistence-unit name="PrototypeCRMSystem-ejbPU" transaction-type="JTA">
       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       <property name="hibernate.show_sql" value="true" />

      Am I missing something configuration-wise or is there a problem?