IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JPA Java Discussion :

Pointeur Null: EJB + JSF


Sujet :

JPA Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mars 2011
    Messages : 22
    Par défaut Pointeur Null: EJB + JSF
    hello tout le monde,
    bon, je m'explique j'ai un package model qui contient mes classes métiers "Entity" + DAO "Session Bean" (Générées avec Hibernate depuis la BDD).
    Et ce que j'essaye de faire c'est de persister une instance de mon modéle en passant par une page JSF bien entendu, mais ça passe pas puisque j'obtiens une erreur de type NullPointerException. Pourtant mes objest sont bien instanciés.

    En gros voici la strucutre:

    model.Annonceur:
    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
    @Entity
    @Table(name = "annonceur", catalog = "annoncesee")
    public class Annonceur implements java.io.Serializable {
     
    	private Integer idannonceur;
    	private String nom;
    	private String prenom;
    	private String adresse;
    	private String ville;
    	private int tel;
    	private String email;
    	private String identifiant;
    	private String pass;
    	private int anneenaiss;
     
    	public Annonceur() {
    	}
     
    	public Annonceur(String nom, String prenom, String adresse, String ville,
    			int tel, String email, String identifiant, String pass,
    			int anneenaiss) {
    		this.nom = nom;
    		this.prenom = prenom;
    		this.adresse = adresse;
    		this.ville = ville;
    		this.tel = tel;
    		this.email = email;
    		this.identifiant = identifiant;
    		this.pass = pass;
    		this.anneenaiss = anneenaiss;
    	}
     
    	@Id
    	@GeneratedValue(strategy = IDENTITY)
    	@Column(name = "idannonceur", unique = true, nullable = false)
    	public Integer getIdannonceur() {
    		return this.idannonceur;
    	}
     
    	public void setIdannonceur(Integer idannonceur) {
    		this.idannonceur = idannonceur;
    	}
     
    	@Column(name = "nom", nullable = false, length = 30)
    	public String getNom() {
    		return this.nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
     
    	@Column(name = "prenom", nullable = false, length = 30)
    	public String getPrenom() {
    		return this.prenom;
    	}
     
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
     
    	@Column(name = "adresse", nullable = false, length = 65535)
    	public String getAdresse() {
    		return this.adresse;
    	}
     
    	public void setAdresse(String adresse) {
    		this.adresse = adresse;
    	}
     
    	@Column(name = "ville", nullable = false, length = 30)
    	public String getVille() {
    		return this.ville;
    	}
     
    	public void setVille(String ville) {
    		this.ville = ville;
    	}
     
    	@Column(name = "tel", nullable = false)
    	public int getTel() {
    		return this.tel;
    	}
     
    	public void setTel(int tel) {
    		this.tel = tel;
    	}
     
    	@Column(name = "email", nullable = false, length = 40)
    	public String getEmail() {
    		return this.email;
    	}
     
    	public void setEmail(String email) {
    		this.email = email;
    	}
     
    	@Column(name = "identifiant", nullable = false, length = 30)
    	public String getIdentifiant() {
    		return this.identifiant;
    	}
     
    	public void setIdentifiant(String identifiant) {
    		this.identifiant = identifiant;
    	}
     
    	@Column(name = "pass", nullable = false, length = 30)
    	public String getPass() {
    		return this.pass;
    	}
     
    	public void setPass(String pass) {
    		this.pass = pass;
    	}
     
    	@Column(name = "anneenaiss", nullable = false)
    	public int getAnneenaiss() {
    		return this.anneenaiss;
    	}
     
    	public void setAnneenaiss(int anneenaiss) {
    		this.anneenaiss = anneenaiss;
    	}
    model.AnnoceurHome: (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
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    @Stateless
    public class AnnonceurHome {
     
    	private static final Log log = LogFactory.getLog(AnnonceurHome.class);
     
    	@PersistenceContext
    	private EntityManager entityManager;
     
    	public void persist(Annonceur transientInstance) {
    		log.debug("persisting Annonceur instance");
    		try {
    			entityManager.persist(transientInstance);
    			log.debug("persist successful");
    		} catch (RuntimeException re) {
    			log.error("persist failed", re);
    			throw re;
    		}
    	}
     
    	public void remove(Annonceur persistentInstance) {
    		log.debug("removing Annonceur instance");
    		try {
    			entityManager.remove(persistentInstance);
    			log.debug("remove successful");
    		} catch (RuntimeException re) {
    			log.error("remove failed", re);
    			throw re;
    		}
    	}
     
    	public Annonceur merge(Annonceur detachedInstance) {
    		log.debug("merging Annonceur instance");
    		try {
    			Annonceur result = entityManager.merge(detachedInstance);
    			log.debug("merge successful");
    			return result;
    		} catch (RuntimeException re) {
    			log.error("merge failed", re);
    			throw re;
    		}
    	}
     
    	public Annonceur findById(Integer id) {
    		log.debug("getting Annonceur instance with id: " + id);
    		try {
    			Annonceur instance = entityManager.find(Annonceur.class, id);
    			log.debug("get successful");
    			return instance;
    		} catch (RuntimeException re) {
    			log.error("get failed", re);
    			throw re;
    		}
    	}
    }
    controlleur.persisterANN: (mon controlleur)
    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
    package controlleur;
     
    import model.Annonceur;
    import model.AnnonceurHome;
     
    public class persisterANN {
    	private Annonceur an;
    	private AnnonceurHome anH;
     
    	public persisterANN() {
    		// TODO Auto-generated constructor stub
    	}
     
    	public Annonceur getAn() {
    		return an;
    	}
     
    	public void setAn(Annonceur an) {
    		this.an = an;
    	}
     
    	public AnnonceurHome getAnH() {
    		return anH;
    	}
     
    	public void setAnH(AnnonceurHome anH) {
    		this.anH = anH;
    	}
     
    	public String insertBDD(){
    		an = new Annonceur("jack","raiden","gaming town","tokyo",0522,"athot","guer","azerty",1989);
    		anH = new AnnonceurHome();
    		anH.persist(an);
    		return "nice";
    	}
     
    }
    persister.jsp: (ma vue)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <body>
    <f:view>
    <h:form>
    <h:outputText value="Persister Annonceur:" />
    <h:commandButton value="Persister" action="#{persisterANN.insertBDD }" />
    </h:form>
    </f:view>
    </body>
    Mon erreur:
    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
    ERROR [AnnonceurHome] persist failed
    java.lang.NullPointerException
    	at model.AnnonceurHome.persist(AnnonceurHome.java:26)
    	at controlleur.persisterANN.insertBDD(persisterANN.java:33)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.el.parser.AstValue.invoke(AstValue.java:170)
    	at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    	at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
    	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    	at javax.faces.component.UICommand.broadcast(UICommand.java:387)
    	at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
    	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
    	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    	at java.lang.Thread.run(Thread.java:662)
    15:00:06,250 ERROR [application] java.lang.NullPointerException
    javax.faces.el.EvaluationException: java.lang.NullPointerException
    	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    	at javax.faces.component.UICommand.broadcast(UICommand.java:387)
    	at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
    	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
    	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    	at java.lang.Thread.run(Thread.java:662)
    Caused by: java.lang.NullPointerException
    	at model.AnnonceurHome.persist(AnnonceurHome.java:26)
    	at controlleur.persisterANN.insertBDD(persisterANN.java:33)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.el.parser.AstValue.invoke(AstValue.java:170)
    	at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    	at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
    	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    	... 28 more
    Et Merci d'avance de votre aide.

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    entityManager est null. Es tu sur que tu as un contexte EJB de disponible et une version de JSF compatible avec les EJB?

  3. #3
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mars 2011
    Messages : 22
    Par défaut
    à priori oui, je suis sous eclipse avec la configuration suivante:

    - eclipse Helios.
    - JBoss Tools 3.2.2
    - JSF 2.0
    - Serveur d'application JBOSS 5

    Petite précision: j'ai crée un projet de type JSF et non pas un dynamic web project.

  4. #4
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mars 2011
    Messages : 22
    Par défaut
    voilà, j'ai même essayé une exécution JAVA sans pour autant passer par des pages JSF mais que dalle. Je suis vraiment paumé...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class run {
    	public static void main(String[] args){
    		persisterANN p = new persisterANN();
    		String msg = p.insertBDD();
    		if(msg!=null){
    			System.out.println(""+msg);
    		}
    		else
    			System.out.println("Fail !");
     
    	}
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Exception in thread "main" java.lang.NullPointerException
    	at model.AnnonceurHome.persist(AnnonceurHome.java:26)
    	at controlleur.persisterANN.insertBDD(persisterANN.java:35)
    	at controlleur.run.main(run.java:6)

  5. #5
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Pour que ton entitymanager soit injecter, il faut être dans un contexte EJB, avec un conteneur EJB, ton application "main" n'en est clairement pas un.

  6. #6
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mars 2011
    Messages : 22
    Par défaut
    Voilà, l'entitymanager est null car je suis pas dans un context EJB! du coup j'ai essayé d'ajouter l'annotation EJB comme suit:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    public class persisterANN {
    	@EJB
    	private AnnonceurHome anH;
    Pourtant j'obtiens une erreur lors du deploiement

    J'ai trouvé une autre alternative: déclarer un entitymanagerFactory mais il faut lui passer le nom de la "persistence-unit" or moi j'ai pas un fichier persistence.xml car j'ai opté pour le "reverse engineering". Donc si ya une solution afin de passer par un entityManagerFactory je suis preneur.

    Sinon merci tchize pour tes réponses.

Discussions similaires

  1. Réponses: 1
    Dernier message: 14/03/2006, 09h13
  2. [PORT COM] RS485 et pointeur null...
    Par floanne dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 20/02/2006, 11h00
  3. get => pointeur null apres fermeture d'une sous-fenetre
    Par gorgonite dans le forum AWT/Swing
    Réponses: 15
    Dernier message: 11/02/2006, 21h42
  4. [Info][Mémoire] utilisée pour un pointeur null
    Par thomas_strass dans le forum Langage
    Réponses: 14
    Dernier message: 04/11/2004, 12h48
  5. Réponses: 4
    Dernier message: 06/04/2004, 21h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo