Erreur "save failed: java.lang.NullPointerException"
Mon projet est testStruts2 j'utilise l'IDE MyEclipse8.6, Serveur d'application: Jboss6 et MySQL pour la base de données.Les technologies utilisées sont : EJB3 struts2 ,JPA:
Lors de l'exécution de l'ajout d'un utilisateur , et en suivant les étapes de debug l'instruction "entityManager.persist(entity)" ne passe pas et les erreurs suivantes sont affichées:
Code:
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
| 13:25:35,435 INFO [testStruts2EJBPU] saving Utilisateur instance
GRAVE [testStruts2EJBPU] save failed: java.lang.NullPointerException
at com.projet.ejb.UtilisateurFacade.save(UtilisateurFacade.java:71) [:](entityManager.persist(entity);)
at com.projet.struts.LoginAction.execute(LoginAction.java:113) [:](utilisateurFacade.save(utilisateur);)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_13]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_13]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_13]
at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_13]
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243) [:]
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165) [:]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252) [:]
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) [:]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) [:]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) [:]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138) [:]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176) [:]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) [:]
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) [:]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488) [:]
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) [:]
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) [:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
at java.lang.Thread.run(Thread.java:619) [:1.6.0_13] |
Mon code est le suivant:
Ma page JSP:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<s:form action="login">
<s:textfield name="loginUser"
label="Login" >
</s:textfield>
<s:textfield name="pwdUser"
label="Password">
</s:textfield>
<s:textfield name="nomUser"
label="Nom User">
</s:textfield>
<s:textfield name="prenomUser"
label="prenom User">
</s:textfield>
<s:submit/>
</s:form>
</body> |
Ma classe LoginAction.java:
Code:
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
| package com.projet.struts;
import java.sql.SQLException;
import javax.naming.NamingException;
import com.projet.ejb.Utilisateur;
import com.projet.ejb.UtilisateurFacade;
import com.projet.ejb.UtilisateurId;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
String loginUser;
String pwdUser;
String nomUser;
String prenomUser;
UtilisateurId utilisateurId= new UtilisateurId();
Utilisateur utilisateur = new Utilisateur();
UtilisateurFacade utilisateurFacade= new UtilisateurFacade();
public String getLoginUser() {
return loginUser;
}
public void setLoginUser(String loginUser) {
this.loginUser = loginUser;
}
public String getPwdUser() {
return pwdUser;
}
public void setPwdUser(String pwdUser) {
this.pwdUser = pwdUser;
}
public String getNomUser() {
return nomUser;
}
public void setNomUser(String nomUser) {
this.nomUser = nomUser;
}
public String getPrenomUser() {
return prenomUser;
}
public void setPrenomUser(String prenomUser) {
this.prenomUser = prenomUser;
}
public String execute()
{
System.out.println(getLoginUser());
System.out.println(getPwdUser());
utilisateurId.setLoginUtilisateur(getLoginUser());
utilisateurId.setPasswordUtilisateur(getPwdUser());
utilisateurId.setPasswordUtilisateur(getNomUser());
utilisateurId.setPasswordUtilisateur(getPrenomUser());
utilisateur.setId(utilisateurId);
try {
utilisateurFacade.save(utilisateur);
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
} |
Mon fichier struts.xml:
Code:
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="com.projet.struts" extends="struts-default">
<action name="login" class="com.projet.struts.LoginAction">
<result name="success">/index.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts> |
Ma classe save UtilisateurFacade.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class UtilisateurFacade {
// property constants
@PersistenceContext(unitName="testStruts2EJBPU")
private EntityManager entityManager;
public void save(Utilisateur entity) throws NamingException, SQLException, ClassNotFoundException {
LogUtil.log("saving Utilisateur instance", Level.INFO, null);
try {
Context context = new InitialContext();
DataSource source= (DataSource) context.lookup("java:/testStruts2");
Connection conn= source.getConnection();
if(conn.isValid(5)){
System.out.println("connexion valide");
entityManager.persist(entity);
LogUtil.log("save successful", Level.INFO, null);
}
} catch (RuntimeException re) {
LogUtil.log("save failed", Level.SEVERE, re);
throw re;
}
}
} |
Mon fichier persistance.xml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8"?>
<persistence 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" version="1.0">
<persistence-unit name="testStruts2EJBPU" transaction-type="JTA">
<jta-data-source>java:/testStruts2</jta-data-source>
<class>com.projet.ejb.UtilisateurFacade</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="admin"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/intranet"/>
</properties>
</persistence-unit>
</persistence> |
et mon fichier mysql-ds.xml qui se trouve sous: Jboss_Home\server\default\deploy:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>testStruts2</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/intranet</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>admin</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources> |
Merci de m'aider à trouver une solution