La liste de jsf renvoie null

Fermé
MaoFofana Messages postés 6 Date d'inscription samedi 10 février 2018 Statut Membre Dernière intervention 18 juillet 2018 - Modifié le 18 juil. 2018 à 15:23
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 18 juil. 2018 à 21:52
S'il vous plaît, je besoin de votre aide. J'ai crée le'element h: selectOneMenu dans ma page mais il ne retourne pas de valeur dans mon bean; voila mo code jsf

<ui:repeat value="#{localCahiersJpaController.findLocalCahiersEntities()}" var="elementCahier" >
                                                <h:form> <div class="col-md-4 product-men">
                                                    <div class="men-pro-item simpleCart_shelfItem">
        <div class="men-thumb-item">
         <h:graphicImage library="img" name="m1.jpg" />
         
         <span class="product-new-top">#{elementCahier.prix}f</span>
        </div>
        <div class="item-info-product ">
         <h4>
                                                                            <a href="single.html">#{elementCahier.nom}</a>
         </h4>
         <div class="info-product-price">
                                                                            <span class="item_price">#{elementCahier.prix}fcfa/ </span>
                                                                            <span class="item_price">#{elementCahier.page}</span>
         </div>
                                                                    
                                                                        <div class="info-product-price">
                                                                            #{controleAchat.maQuantites}
                                                                            Quantités : 
                                                                            <h:selectOneMenu value="#{controleAchat.maQuantites}" required="true">
                                                                                <f:selectItem itemLabel="0" itemValue="0" />
                                                                                <f:selectItem itemLabel="1" itemValue="1" />
                                                                                <f:selectItem itemLabel="2" itemValue="2" />
                                                                                <f:selectItem itemLabel="3" itemValue="3" />
                                                                                <f:selectItem itemLabel="4" itemValue="4" />
                                                                                <f:selectItem itemLabel="6" itemValue="6" />
                                                                                <f:selectItem itemLabel="7" itemValue="7" />
                                                                                <f:selectItem itemLabel="8" itemValue="8" />
                                                                                <f:selectItem itemLabel="9" itemValue="9" />
                                                                                <f:selectItem itemLabel="10" itemValue="10" />
                                                                              </h:selectOneMenu>
                                                                            
                                                                        </div> 
                                                                        
         <div class="snipcart-details top_brand_home_details item_add single-item hvr-outline-out">
                                                                            <h:commandButton action="#{controleAchat.ajouCahierPanier(controleAchat.maQuantites,elementCahier)}"   value="Ajoutez au panier" styleClass="btn btn-primary" />                                  
                                                                            
                                                                        </div>

        </div>
                                                        </div>
      </div></h:form> 
                                            </ui:repeat>



et voilà mon code javabean
@ManagedBean
@SessionScoped
public class ControleAchat {   
    private int compteurPanier; 
    private String maQuantites;
    public GestionLien lien  = new GestionLien(); 
    
   Cahiers cahier = new Cahiers();
   Divers divers = new Divers();
   Livres livres = new Livres();
   Sacs sac = new Sacs();
   
   
    private List<Cahiers>  itemCahier = new ArrayList<>() ;
    private List<Divers>  itemDivers = new ArrayList<>() ;
    private List<Livres>  itemLivre = new ArrayList<>() ;
    private List<Sacs>  itemSacs = new ArrayList<>() ;
    
    
    public ControleAchat(){}

    public ControleAchat(int compteurPanier, String maQuantites) {
        this.compteurPanier = compteurPanier;
        this.maQuantites = maQuantites;
    }
    
    //Ajout
    public String ajouCahierPanier(LocalCahiers local){
            
                   
            int i = Integer.valueOf(this.maQuantites).intValue();
            
        if(i != 0){
             int prixTotal = local.getPrix() * 1;
           
            cahier = new Cahiers(local.getNom(),local.getPage(), 1,
                         local.getPrix(),prixTotal);
            
            this.compteurPanier += 1;
            itemCahier.add(cahier);
            this.maQuantites = null;
    }
            return lien.lienCahier();
       }
    
   /* 
    public String ajouDiversPanier(LocalDivers local){
           Integer monnombre=new Integer(this.quantites);
            int i = monnombre.intValue();
            if(i != 0){
                
            int prixTotal= local.getPrix() * local.getQuantites();
           divers = new Divers(local.getNom(),i,local.getPrix(),prixTotal);
            itemDivers.add(divers);
            this.compteurPanier += 1;
            this.quantites = null;}
            return lien.lienDivers();
         }
    
    public String ajouLivresPanier(LocalLivre local){
           Integer monnombre=new Integer(this.quantites);
            int i = monnombre.intValue();
            if(i != 0){
            
            int prixTotal= local.getPrix() * local.getQuantites();
            
            livres = new Livres(local.getNom(),local.getIdInfoLivres().getClasse()
                    ,local.getQuantites(), local.getPrix(),local.getPrixTotal());
           
            itemLivre.add(livres);
            this.compteurPanier += 1;
            this.quantites = null;}
            return lien.lienLivres();
         }
    
    public String ajouSacsPanier(LocalSac local){
        Integer monnombre=new Integer(this.quantites);
            int i = monnombre.intValue();
            if(i != 0){
            int prixTotal= local.getPrix() * local.getQuantites();
            sac = new Sacs(local.getNom(),i,local.getPrix(),prixTotal);
            itemSacs.add(sac);
            this.compteurPanier += 1;
            this.quantites = null;}
            return lien.lienSacs();
         }
    
    */
    
    //Suppression 
    public String sppressionCahier(Cahiers local){
         itemCahier.remove(local) ;
        this.compteurPanier -= 1;
         return lien.lienPanier();
    }
    
    public String sppressionDivers(Divers local){
         itemDivers.remove(local) ;
        this.compteurPanier -= 1;
         return lien.lienPanier();
    }
    
    
    
    public String sppressionLivres(Livres local){
         itemLivre.remove(local) ;
         this.compteurPanier -= 1;
        return lien.lienPanier();
    }
    
    public String sppressionSacs(Sacs local){
         itemSacs.remove(local) ;
         this.compteurPanier -= 1;
        return lien.lienPanier();
    }
   
  
    public int getCompteurPanier() {
        return compteurPanier;
    }

    public void setCompteurPanier(int compteurPanier) {
        this.compteurPanier = compteurPanier;
    }

    public String getMaQuantites() {
        return maQuantites;
    }

    public void setMaQuantites(String maQuantites) {
        this.maQuantites = maQuantites;
    }

    

    public Cahiers getCahier() {
        return cahier;
    }

    public void setCahier(Cahiers cahier) {
        this.cahier = cahier;
    }

    public Divers getDivers() {
        return divers;
    }

    public void setDivers(Divers divers) {
        this.divers = divers;
    }

    public Livres getLivres() {
        return livres;
    }

    public void setLivres(Livres livres) {
        this.livres = livres;
    }

    public Sacs getSac() {
        return sac;
    }

    public void setSac(Sacs sac) {
        this.sac = sac;
    }

    public List<Cahiers> getItemCahier() {
        return itemCahier;
    }

    public void setItemCahier(List<Cahiers> itemCahier) {
        this.itemCahier = itemCahier;
    }

    public List<Divers> getItemDivers() {
        return itemDivers;
    }

    public void setItemDivers(List<Divers> itemDivers) {
        this.itemDivers = itemDivers;
    }

    public List<Livres> getItemLivre() {
        return itemLivre;
    }

    public void setItemLivre(List<Livres> itemLivre) {
        this.itemLivre = itemLivre;
    }

    public List<Sacs> getItemSacs() {
        return itemSacs;
    }

    public void setItemSacs(List<Sacs> itemSacs) {
        this.itemSacs = itemSacs;
    }
    
    
    
   
}

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
18 juil. 2018 à 15:45
Bonjour,

Par défaut la méthode setMaQuantites devrait être appelé au moment de la validation du formulaire, c'est à dire quand tu appuies sur le bouton, sauf que quand tu appelles ce bouton tu appelles une méthode ajouCahierPanier qui prends 2 paramètres (alors que dans ton bean elle n'en a qu'un) et dont la valeur des paramètres correspond à l'état des variables avant que le bouton n'ait été appelé, c'est à dire sans prendre en compte le changement de maQuantites.

Il faudrait plutôt laisser JSF faire son travail normalement, c'est à dire appeler automatiquement le setMaQuantites lors de l'appui sur le bouton et dans l'action du bouton ne pas utiliser la valeurs des paramètres mais directement regarder l'état des variables qu'il vient d'être modifié.

Remarque : je t'invites à ajouter des logs dans tes méthodes afin de tracer quand chacune de ces méthodes (get, set et actions) sont appelées, en rajoutant éventuellement dans les logs l'état de ton objet, ce sera plus clair.
1
MaoFofana Messages postés 6 Date d'inscription samedi 10 février 2018 Statut Membre Dernière intervention 18 juillet 2018
Modifié le 18 juil. 2018 à 21:42
Merci pour la reponce , mais j'avais faire des modification avant de poster sur le forum voici le code initial

 <ui:repeat value="#{infoCahierJpaController.findInfoCahierEntities()}" var="elementInfo" >
     <div class="product-sec1">
                                            <h3 class="heading-tittle"><h:outputText id="Maona" value="#{elementInfo.categorie}" /> </h3>
                                            <ui:repeat value="#{localCahiersJpaController.findLocalCahiersEntities()}" var="elementCahier"  >
                                                <h:form> <div class="col-md-4 product-men">
                                                    <div class="men-pro-item simpleCart_shelfItem">
        <div class="men-thumb-item">
         <h:graphicImage library="img" name="m1.jpg" />
         
         <span class="product-new-top">#{elementCahier.prix}f</span>
        </div>
        <div class="item-info-product ">
         <h4>
                                                                            <a href="single.html">#{elementCahier.nom}</a>
         </h4>
         <div class="info-product-price">
                                                                            <span class="item_price">#{elementCahier.prix}fcfa/ </span>
                                                                            <span class="item_price">#{elementCahier.page}</span>
         </div>
                                                                    
                                                                        <div class="info-product-price">
                                                                            
                                                                            Quantités : <h:selectOneMenu value="#{controleAchat.maQuantites}" required="true">
                                                                                <f:selectItem itemLabel="0" itemValue="0" />
                                                                                <f:selectItem itemLabel="1" itemValue="1" />
                                                                                <f:selectItem itemLabel="2" itemValue="2" />
                                                                                <f:selectItem itemLabel="3" itemValue="3" />
                                                                                <f:selectItem itemLabel="4" itemValue="4" />
                                                                                <f:selectItem itemLabel="6" itemValue="6" />
                                                                                <f:selectItem itemLabel="7" itemValue="7" />
                                                                                <f:selectItem itemLabel="8" itemValue="8" />
                                                                                <f:selectItem itemLabel="9" itemValue="9" />
                                                                                <f:selectItem itemLabel="10" itemValue="10" />
                                                                              </h:selectOneMenu>
                                                                            
                                                                        </div> 
                                                                        
         <div class="snipcart-details top_brand_home_details item_add single-item hvr-outline-out">
                                                                            <h:commandButton action="#{controleAchat.ajouCahierPanier(elementCahier)}"   value="Ajoutez au panier" styleClass="btn btn-primary">                                  
                                                                                
                                                                            </h:commandButton>
                                                                        </div>

        </div>
                                                        </div>
      </div></h:form> 
                                            </ui:repeat>
         <div class="clearfix"></div>
     </div>
     
                                        </ui:repeat>



et voici mon javaBean


@ManagedBean
@SessionScoped
public class ControleAchat {   
    private int compteurPanier; 
    private String maQuantites;
    public GestionLien lien  = new GestionLien(); 
    
   Cahiers cahier = new Cahiers();
   Divers divers = new Divers();
   Livres livres = new Livres();
   Sacs sac = new Sacs();
   
   
    private List<Cahiers>  itemCahier = new ArrayList<>() ;
    private List<Divers>  itemDivers = new ArrayList<>() ;
    private List<Livres>  itemLivre = new ArrayList<>() ;
    private List<Sacs>  itemSacs = new ArrayList<>() ;
    
    
    public ControleAchat(){}

    public ControleAchat(int compteurPanier, String maQuantites) {
        this.compteurPanier = compteurPanier;
        this.maQuantites = maQuantites;
    }
    
    //Ajout
    public String ajouCahierPanier(LocalCahiers local){
            
                   
            String montexte = new String(this.maQuantites);
            Integer monnombre=new Integer(montexte);
            int i = monnombre.intValue(); 
        if(i != 0){
             int prixTotal = local.getPrix() * i;
           
            cahier = new Cahiers(local.getNom(),local.getPage(), i,
                         local.getPrix(),prixTotal);
            
            this.compteurPanier += 1;
            itemCahier.add(cahier);
            this.maQuantites = null;
    }
            return lien.lienCahier();
       }
    
   /* 
    public String ajouDiversPanier(LocalDivers local){
           Integer monnombre=new Integer(this.quantites);
            int i = monnombre.intValue();
            if(i != 0){
                
            int prixTotal= local.getPrix() * local.getQuantites();
           divers = new Divers(local.getNom(),i,local.getPrix(),prixTotal);
            itemDivers.add(divers);
            this.compteurPanier += 1;
            this.quantites = null;}
            return lien.lienDivers();
         }
    
    public String ajouLivresPanier(LocalLivre local){
           Integer monnombre=new Integer(this.quantites);
            int i = monnombre.intValue();
            if(i != 0){
            
            int prixTotal= local.getPrix() * local.getQuantites();
            
            livres = new Livres(local.getNom(),local.getIdInfoLivres().getClasse()
                    ,local.getQuantites(), local.getPrix(),local.getPrixTotal());
           
            itemLivre.add(livres);
            this.compteurPanier += 1;
            this.quantites = null;}
            return lien.lienLivres();
         }
    
    public String ajouSacsPanier(LocalSac local){
        Integer monnombre=new Integer(this.quantites);
            int i = monnombre.intValue();
            if(i != 0){
            int prixTotal= local.getPrix() * local.getQuantites();
            sac = new Sacs(local.getNom(),i,local.getPrix(),prixTotal);
            itemSacs.add(sac);
            this.compteurPanier += 1;
            this.quantites = null;}
            return lien.lienSacs();
         }


*/        //Suppression     public String sppressionCahier(Cahiers local){         itemCahier.remove(local) ;        this.compteurPanier -= 1;         return lien.lienPanier();    }        public String sppressionDivers(Divers local){         itemDivers.remove(local) ;        this.compteurPanier -= 1;         return lien.lienPanier();    }                public String sppressionLivres(Livres local){         itemLivre.remove(local) ;         this.compteurPanier -= 1;        return lien.lienPanier();    }        public String sppressionSacs(Sacs local){         itemSacs.remove(local) ;         this.compteurPanier -= 1;        return lien.lienPanier();    }         public int getCompteurPanier() {        return compteurPanier;    }    public void setCompteurPanier(int compteurPanier) {        this.compteurPanier = compteurPanier;    }    public String getMaQuantites() {        return maQuantites;    }    public void setMaQuantites(String maQuantites) {        this.maQuantites = maQuantites;    }        public Cahiers getCahier() {        return cahier;    }    public void setCahier(Cahiers cahier) {        this.cahier = cahier;    }    public Divers getDivers() {        return divers;    }    public void setDivers(Divers divers) {        this.divers = divers;    }    public Livres getLivres() {        return livres;    }    public void setLivres(Livres livres) {        this.livres = livres;    }    public Sacs getSac() {        return sac;    }    public void setSac(Sacs sac) {        this.sac = sac;    }    public List<Cahiers> getItemCahier() {        return itemCahier;    }    public void setItemCahier(List<Cahiers> itemCahier) {        this.itemCahier = itemCahier;    }    public List<Divers> getItemDivers() {        return itemDivers;    }    public void setItemDivers(List<Divers> itemDivers) {        this.itemDivers = itemDivers;    }    public List<Livres> getItemLivre() {        return itemLivre;    }    public void setItemLivre(List<Livres> itemLivre) {        this.itemLivre = itemLivre;    }    public List<Sacs> getItemSacs() {        return itemSacs;    }    public void setItemSacs(List<Sacs> itemSacs) {        this.itemSacs = itemSacs;    }               }


. Et puis desolé mais je comprend pas bien ta proposition au niveau des log
0
MaoFofana Messages postés 6 Date d'inscription samedi 10 février 2018 Statut Membre Dernière intervention 18 juillet 2018
Modifié le 18 juil. 2018 à 21:42
voila l'exception envoye
javax.faces.el.EvaluationException: java.lang.NullPointerException
 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
 at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
 at javax.faces.component.UICommand.broadcast(UICommand.java:330)
 at com.sun.faces.facelets.component.UIRepeat.broadcast(UIRepeat.java:1101)
 at com.sun.faces.facelets.component.UIRepeat.broadcast(UIRepeat.java:1101)
 at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:870)
 at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1418)
 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:201)
 at javax.faces.webapp.FacesServlet.service(FacesServlet.java:670)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
 at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
 at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
 at java.lang.String.<init>(String.java:152)
 at trone.simpleControle.ControleAchat.ajouCahierPanier(ControleAchat.java:46)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
 at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
 at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
 ... 32 more

0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015 > MaoFofana Messages postés 6 Date d'inscription samedi 10 février 2018 Statut Membre Dernière intervention 18 juillet 2018
18 juil. 2018 à 21:52
Il faut la lire l'erreur et faire le lien avec ton code :

java.lang.NullPointerException
 at java.lang.String.<init>(String.java:152)
 at trone.simpleControle.ControleAchat.ajouCahierPanier(ControleAchat.java:46)

Cela correspond à cette ligne là :

    public String ajouCahierPanier(LocalCahiers local){
            String montexte = new String(this.maQuantites); // ControleAchat.java:46

Cela signifie que this.maQuantites vaut null, parce que la méthode setMaQuantites n'a jamais été appelée.

Quant aux logs dont je parlais ils s'agit de rajouter des lignes dans ton fichier de logs qui te permettra de suivre le comportement du programme.

Exemple :

    public String getMaQuantites() {
        Logger.getGlobal().info("getMaQuantites: " + maQuantites); // import java.util.logging.Logger;
        return maQuantites;
    }

    public void setMaQuantites(String maQuantites) {
        Logger.getGlobal().info("setMaQuantites: " + this.maQuantites + " -> " + maQuantites);
        this.maQuantites = maQuantites;
    }

Dans ton fichier de logs tu pourras suivre les appels des méthodes et constater que ton set n'est pas appelé...
0