8

Here's the code insidy my ApplicationContext.xml

    <context:spring-configured />
<context:annotation-config />
<context:component-scan base-package="com.apsas.jpa" />
<tx:annotation-driven />

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="testjpa" />
</bean>

<bean id="entityManager"
    class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

and here's my Dao Implementation

public class TeacherDaoImpl implements TeacherDao {

@Autowired
private EntityManager entityManager;

@Transactional
public Teacher addTeacher(Teacher teacher) {
    entityManager.persist(teacher);
    return teacher;

}

}

Here's my Main Class

public class TestApp {

public static void main(String[] args) {

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "config/ApplicationContext.xml");       

    TeacherDao teacherDao = new TeacherDaoImpl();       
    Teacher teacher1 =  teacherDao.addTeacher(new Teacher("First Teacher"));

}

}

Please help, i am getting a null pointer exception

Exception in thread "main" java.lang.NullPointerException
at com.apsas.jpa.dao.impl.TeacherDaoImpl.addTeacher(TeacherDaoImpl.java:22)
at com.apsas.jpa.main.TestApp.main(TestApp.java:26)

ive been solving this problem in 2 days but still i cant find any resources that may solve this problem. i appreciate if you give me your opinion,answers or any idea that might help me solving this,

ps: i am new on learning spring

Ernest Hilvano
  • 503
  • 3
  • 8
  • 16

2 Answers2

5

Since you are instantiating TeacherDaoImpl yourself (with the new keyword) within main, Spring is not injecting the EntityManager and hence the NPE.

Annotate the field TeacherDaoImpl.entityManager with @PersistenceContext and annotate the TeacherDaoImpl class with @Component to have Spring instantiate it for you. Then in your main, get a hold of that bean:

TeacherDao dao = applicationContext.getBean(TeacherDao.class);
// ...

Also these two directives seem to be unnecessary:

<context:annotation-config />
<context:spring-configured />

The former is implied when you are using <context:component-scan />. The latter is only useful if you are using @Configurable in your code.

Jukka
  • 4,583
  • 18
  • 14
  • Hi Thanks, seems im near on solving this problem, new error came up org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined – Ernest Hilvano Oct 01 '13 at 07:02
  • Do you know what are the exact class that will be added on bean as a transactionmanager? – Ernest Hilvano Oct 01 '13 at 07:03
  • Its now working, i just added – Ernest Hilvano Oct 01 '13 at 07:12
  • Next Question, How will i automatically use the TeacherDao using anotationt, without using of = applicationContext.getBean(TeacherDao.class); – Ernest Hilvano Oct 01 '13 at 07:13
  • If you wish to use the new keyword and still have dependencies injected, google for @Configurable and AspectJ with Spring. Raise a new stackoverflow question if necessary. But do spend some time/thought on it first. – Jukka Oct 01 '13 at 07:19
  • Anywat, just wanted to say thank you very much. :) im glad its now working, maybe i cant do an autoinject because im using it on J2SE, maybe @Autowired will work on JEE – Ernest Hilvano Oct 01 '13 at 07:24
3

You will want to use @PersistenceContext for injecting the EntityManager.

See

PersistenceContext EntityManager injection NullPointerException

which is pretty much the same question.

Community
  • 1
  • 1