annotated entity manager null pointed exception

10,196

Solution 1

Injection of resources (in your case via use of @PersistenceContext) works only in container managed classes (like EJBs and Servlets). This explained with more details for example in Java EE specification v6, EE5.2.5.

What you can do:

  • Modify your class so that it is managed class
  • move injection of resources to managed class and pass it to EmployeeDao,
  • use JNDI lookup as before

Solution 2

Use the annotation javax.ejb.Stateless for your EmployeeDao and IEmployeeDao classes. Entitymanager is a no-interface an Enterprise Java Bean injected in your client.

The client must be either a web component or another enterpise bean. See here for further details about how to use EJBs.

In other words, using the Stateless annotation, the web container will take care of the lifecycle of your EmployeeDao class.

Share:
10,196
user414967
Author by

user414967

Updated on June 04, 2022

Comments

  • user414967
    user414967 almost 2 years

    I am doing a small project using JPA. I need to insert the employee object. For that when I use the annotated entity manager I got the NullPointer exception. But when I use the Normal EntityManager without using the annotation it is working fine. Do I need to configure somewhere else other than persistence.xml to work this examle fine?

    Please see the code below.

    public class EmployeeDao implements IEmployeeDao{       
    
         @PersistenceContext(unitName = "timesheet")
        private EntityManager entityManager ;
    
        @Override
        public boolean createEmployee(IEmployee employee) { 
    
            this.entityManager.persist(employee);       
    
            return true;
        }
    }
    

    persistence.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <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" xmlns="http://java.sun.com/xml/ns/persistence">
        <persistence-unit name="timesheet" transaction-type="RESOURCE_LOCAL">       
            <class>com.timesheet.model.Employee</class>     
            <properties>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
                <property name="javax.persistence.jdbc.url"
                    value="jdbc:mysql://localhost:3306/timesheet" />
                <property name="javax.persistence.jdbc.user" value="root" />
                <property name="javax.persistence.jdbc.password" value="" />
    
                <!-- EclipseLink should create the database schema automatically -->
                <property name="eclipselink.ddl-generation" value="create-tables" />
                <property name="eclipselink.ddl-generation.output-mode"
                    value="database" />
            </properties>
    
        </persistence-unit>
    </persistence>