bonjour,
je tombe sur l'erreur suivante lors du test du projet : java.lang.NullPointerException sur la ligne du findAll du fichier ci-dessous : cela signifie que l'injection de dépendance ne se fait pas, et plus particulièrement l'injection dans la fabrique de gestionnaire d'entité (EntityManagerFactory).
je peux dire cela au vu des lignes concernées par l'erreur.
pourquoi cette erreur?
voici la classe de test:
vioci la classe de DAO:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class InitDB { private static IEmployeDao employeDao=null; private static ICotisationDao cotisationDao=null; private static IIndemniteDao indemniteDao=null; @BeforeClass public static void init(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config-dao.xml"); employeDao =(IEmployeDao) ctx.getBean("employeDao"); cotisationDao=(ICotisationDao) ctx.getBean("cotisationDao"); indemniteDao=(IIndemniteDao) ctx.getBean("indemniteDao"); } @Test public void initDB(){ for (Cotisation cotisation : cotisationDao.findAll()){ cotisationDao.destroy(cotisation); } //remplissage Cotisation cot=new Cotisation(1.2, 1.3, 3.1, 3.2); cotisationDao.create(cot); //affichage du contenu for (Cotisation cot2:cotisationDao.findAll()){ System.out.println(cot2); } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class CotisationDao implements ICotisationDao{ public EntityManagerFactory entityManagerFactory; public EntityManager em; public void setEntityManagerFactory(EntityManagerFactory emf) { this.entityManagerFactory = emf; } public CotisationDao() { } public Cotisation create(Cotisation employe) { em=entityManagerFactory.createEntityManager(); try{ em.persist(employe); }catch (Exception e){ throw new PamException("erreur dans CotisationDao - create / "+e.getMessage(), 1); } em.close(); return employe; } public Cotisation edit(Cotisation employe) { em=entityManagerFactory.createEntityManager(); try{ employe=em.merge(employe); }catch (Exception e){ throw new PamException("erreur dans CotisationDao - edit / "+e.getMessage(), 1); } em.close(); return employe; } public void destroy(Cotisation employe) { em=entityManagerFactory.createEntityManager(); try{ em.remove(employe); em.close(); }catch (Exception e){ throw new PamException("erreur dans CotisationDao - destroy / "+e.getMessage(), 1); } } public Cotisation find(Long id) { em=entityManagerFactory.createEntityManager(); Cotisation employe=em.find(Cotisation.class, id); if (employe==null) throw new PamException("erreur dans CotisationDao - find / aucune Cotisation d'id "+id,1); em.close(); return employe; } public List<Cotisation> findAll() { em=entityManagerFactory.createEntityManager(); List<Cotisation> liste=em.createQuery("select c from Cotisation c").getResultList(); em.close(); return liste; } }
et voici le fichier de configuration des beans de spring:
et le persistence.xml:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="employeDao" class="dao.EmployeDao"> <property name="entityManagerFactory" ref="entityManagerFactory"> </property> </bean> <bean id="cotisationDao" class="dao.CotisationDao"> <property name="entityManagerFactory" ref="entityManagerFactory"> </property> </bean> <bean id="indemniteDao" class="dao.IndemniteDao"> <property name="entityManagerFactory" ref="entityManagerFactory"> </property> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="javaee-1PU"></property> </bean> </beans>
je ne mets pas le fichier des entités (une clé primaire, quelques attributs, des constructeurs, getters, setters toString et hashCode).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 <?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="javaee-1PU" transaction-type="RESOURCE_LOCAL"> <!--<provider>org.hibernate.ejb.HibernatePersistence</provider>--> <class>jpa.Employe</class> <class>jpa.Cotisation</class> <class>jpa.Indemnite</class> <properties> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.password" value="password"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jbossdb"/> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence>
voici par contre et pour finir la liste des jars de la bibliothèque:
voir la photo.
merci pour toute aide,
olivier.
Partager