Yop,
je craque, je demande à l'aide
Tout d'abord, je déploie sur GlassFish et j'utilise Toplink et Postgres pour la base. Dans l'ear que je déploie, j'ai deux projet : un projet ejb correspondant au serveur et un projet war correspondant au client web.
Donc mon problème est au niveau de mon client Web dans lequel je n'arrive pas à faire d'injection dans mes classes de mes session local.
Un peu de code :
Une de mes sessions
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 @Local public interface CoordinatesSessionLocal { public List findAll(); }Mon entité :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 @Stateless public class CoordinatesSessionBean implements CoordinatesSessionLocal, Serializable { @PersistenceContext private EntityManager em; public List<CoordinatesEntity> findAll() { return (List<CoordinatesEntity>)em.createQuery("SELECT o FROM CoordinatesEntity o").getResultList(); } }
Côté Client Web : J'ai créé un servlet et une classe de test
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 @Entity @Table(name = "Coordinates") public class CoordinatesEntity implements Serializable { private static final long serialVersionUID = -9036561041664855342L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String address; private String addressComplement; private String postalCode; private String city; @Column(nullable = false) private String phone; @Column(nullable = false) private String email; private String fax; @Version private int version; public CoordinatesEntity() { } public CoordinatesEntity(String address, String addressComplement, String postalCode, String city, String phone, String email, String fax) { this.address = address; this.addressComplement = addressComplement; this.postalCode = postalCode; this.city = city; this.phone = phone; this.email = email; this.fax = fax; } public String getAddress() { return address; } public String getAddressComplement() { return addressComplement; } public String getCity() { return city; } public String getEmail() { return email; } public String getFax() { return fax; } public long getId() { return id; } public String getPhone() { return phone; } public String getPostalCode() { return postalCode; } public int getVersion() { return version; } public void setAddress(String address) { this.address = address; } public void setAddressComplement(String addressComplement) { this.addressComplement = addressComplement; } public void setCity(String city) { if (city == null || city.isEmpty()) { return; } if (Rules.checkCity(city)) { throw new IllegalArgumentException("the format of city isn't valid"); } this.city = city; } public void setEmail(String mail) { if (mail == null || mail.isEmpty()) { throw new IllegalArgumentException("You must specify a mail"); } if (!Rules.checkEmail(mail)) { throw new IllegalArgumentException("the mail isn't valid : it must follow the pattern myname@mydomain.fr for example"); } this.email = mail; } public void setFax(String fax) { if (fax == null || fax.isEmpty()) { return; } if (!Rules.checkFax(phone)) { throw new IllegalArgumentException("the fax number isn't valid : it must contain ten numbers"); } this.fax = fax; } public void setId(long id) { this.id = id; } public void setPhone(String phone) { if (phone == null || phone.isEmpty()) { return; } if (!Rules.checkPhone(phone)) { throw new IllegalArgumentException("the phone number isn't valid : it must contain ten numbers"); } this.phone = phone; } public void setPostalCode(String postalCode) { if (postalCode == null || postalCode.isEmpty()) { return; } if (!Rules.checkPostalCode(postalCode)) { throw new IllegalArgumentException("the postal code isn't valid : it must be composed of five numbers"); } this.postalCode = postalCode; } public void setVersion(int version) { this.version = version; } }
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 public class MyServlet extends HttpServlet { @EJB private CoordinatesSessionLocal coordinatesSession; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { List news = coordiantesSession.findAll(); for (Iterator it = news.iterator(); it.hasNext();) { A elem = (A) it.next(); out.println(" <b> name : " + elem.getEmail() + " </b><br />"); } List news2 = new NewClass().getCoordiantesSessionLocal().findAll(); for (Iterator it = news2.iterator(); it.hasNext();) { A elem = (A) it.next(); out.println(" <b> name : " + elem.getEmail() + " </b><br />"); } } finally { out.close(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 public class NewClass { @EJB private CoordinatesSessionLocal coordinatesSession; public CoordinatesSessionLocal getCoordinatesSessionLocal () { return coordinatesSession; } }
Donc, j'ai des infos dans ma base pour récupérer des coordonnées, ma méthode findAll marche.
Maintenant quand on regarde mon servlet, je vais afficher deux fois les coordonnées, une première fois en utilisant l'injection que je fais dans mon servlet et la deuxieme fois en utilisant celle de ma classe newClass.
La premier affichage marche mais le deuxième me fais un NullPointerException.
Pourquoi mon injection marche dans mon servlet et pas dans ma classe ?file:/D:/Java/Workspace/Netbeans/HIP/dist/gfdeploy/HIP-ejb_jar/-HIP-ejbPU login successful
StandardWrapperValve[MyServlet]: PWC1406: Servlet.service() for servlet MyServlet threw exception
java.lang.NullPointerException
at web.MyServlet.processRequest(MyServlet.java:46)
at web.MyServlet.doGet(MyServlet.java:73)
Partager