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

Hibernate Java Discussion :

liste vide avec l'erreur java.lang.NullPointerException


Sujet :

Hibernate Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Novembre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2015
    Messages : 7
    Points : 3
    Points
    3
    Par défaut liste vide avec l'erreur java.lang.NullPointerException
    Bonjour tout le monde,

    je travaille avec spring, hibernate et jsf (primefaces) ainsi que tomcat 7 et mysql (xampp et mysql workbench)
    ma première page sert à afficher une liste retournée par une méthode et stockée par la variable "listDerivations" (qui a le getter et le setter).
    tout d'abord voici l'architecture du projet.
    Nom : architecture_projet.png
Affichages : 628
Taille : 22,1 Ko
    et voici le code de mes différentes class et interface et fichiers de configuration

    com.model.derivation
    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
    package com.model;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.validation.constraints.NotNull;
     
    @Entity
    @Table(name="derivation") // nom de la table dans la base de données (sans majuscules)
    public class Derivation {
    	@Id
    	@NotNull
    	private int idderivation;
    	@Column
    	private int code_derivation;
    	@Column
    	private String libelle_derivation;
    	@Column
    	private int etat_derivation;
    	// Constructeurs avec et sans arguments
    	// Getters and Setters
    }
    com.service.DerivationService
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    package com.service;
    import java.util.List;
    import com.model.Derivation;
    public interface DerivationService {
    	public List<Derivation> findAll();
    }
    com.service.DerivationServiceImpl
    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
    package com.service;
    import java.util.List;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import com.model.Derivation;
     
    @Service("derivationService")
    @Transactional
    public class DerivationServiceImpl implements DerivationService {
    	@Autowired
    	private SessionFactory sessionFactory;
    	@Override
    	public List<Derivation> findAll() {
    	return (List<Derivation>)this.sessionFactory.getCurrentSession().createQuery("from derivation").list();
    	}
    }
    com.beans.DerivationBean
    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
    package com.beans;
    import java.util.ArrayList;
    import java.util.List;
    import javax.faces.bean.ManagedBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import com.model.Derivation;
    import com.service.DerivationService;
     
    @ManagedBean(name="derivationBean")
    @Scope
    public class DerivationBean {
    	private List<Derivation> listDerivations = new ArrayList<Derivation>();
    	@Autowired 
    	private DerivationService derivationService;
     
    	public List<Derivation> getListDerivations() {
    		listDerivations = derivationService.findAll();
    		//Derivation d = new Derivation();
    		//d.setLibelle_derivation("test");
    		//listDerivations.add(d);
    		return listDerivations;
    	}
    	public void setListDerivations(List<Derivation> listDerivations) {
    		this.listDerivations = listDerivations;
    	}
    }
    application-context.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
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
     
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="driverClass" value="org.gjt.mm.mysql.Driver"></property>
    	<property name="user" value="root"/>
    	<property name="password" value=""/>
    	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/atgdb"></property>
    </bean>
     
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    	<property name="dataSource" ref="dataSource"/>
    	<property name="packagesToScan" value="com.model"/>
    	<property name="hibernateProperties">
    		<props>
    			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    			<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
    		</props>
    	</property>
    </bean>
     
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    	<property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
     
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config/>
    <context:component-scan base-package="com"></context:component-scan>
    </beans>
    web.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
    <?xml version="1.0" encoding="ASCII"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name></display-name>
     
      <context-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>classpath:application-context.xml</param-value>
      </context-param>
     
      <listener>
      	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <listener>
      	<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
      </listener>
     
      <servlet>
      	<servlet-name>Faces Servlet</servlet-name>
      	<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      	<load-on-startup>1</load-on-startup>
      </servlet>
     
      <servlet-mapping>
      	<servlet-name>Faces Servlet</servlet-name>
      	<url-pattern>*.xhtml</url-pattern>
      </servlet-mapping>
     
      <welcome-file-list>
        <welcome-file>listderivations.xhtml</welcome-file>
      </welcome-file-list>
     
      </web-app>
    faces-config.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml version="1.0" encoding="UTF-8"?>
    <faces-config
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
        version="2.0">
     
        <application>
        	<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
        </application>
    </faces-config>
    la page d'affichage listderivations.xhtml
    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
    <html xmlns="http://www.w3.org/1999/xhtml"   
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:p="http://primefaces.org/ui"
          >
    <h:head>
     
    </h:head>
    <h:body>
    <h:outputLabel value="testaffichage"></h:outputLabel>
    		<p:dataTable value="#{derivationBean.listDerivations}" var="derivation">
    			<p:column headerText="Id Derivation">
    				<h:outputText value="#{derivation.idderivation}"/>
    			</p:column>
    			<p:column headerText="Code Derivation">
    				<h:outputText value="#{derivation.code_derivation}"/>
    			</p:column>
    			<p:column headerText="Libelle Derivation">
    				<h:outputText value="#{derivation.libelle_derivation}"/>
    			</p:column>
    			<p:column headerText="Etat Derivation">
    				<h:outputText value="#{derivation.etat_derivation}"/>
    			</p:column>
    		</p:dataTable>
    </h:body>
    </html>
    quand je lance l'application, j'ai l'erreur suivante :
    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
    nov. 15, 2015 1:30:58 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
    GRAVE: Error Rendering View[/listderivations.xhtml]
    javax.el.ELException: /listderivations.xhtml @15,75 value="#{derivationBean.listDerivations}": Error reading 'listDerivations' on type com.beans.DerivationBean
    	at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
    	at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    	at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    	at javax.faces.component.UIData.getValue(UIData.java:732)
    	at org.primefaces.component.api.UIData.getDataModel(UIData.java:759)
    	at javax.faces.component.UIData.getRowCount(UIData.java:356)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:806)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:788)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:281)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:243)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85)
    	at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    	at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
    	at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    	at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    	at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: javax.el.ELException: Error reading 'listDerivations' on type com.beans.DerivationBean
    	at javax.el.BeanELResolver.getValue(BeanELResolver.java:98)
    	at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    	at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    	at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    	at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    	at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    	... 36 more
    Caused by: java.lang.NullPointerException
    	at com.beans.DerivationBean.getListDerivations(DerivationBean.java:30)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
    	... 41 more
     
    nov. 15, 2015 1:30:58 PM org.apache.catalina.core.StandardWrapperValve invoke
    GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/checkingatg] threw exception [null] with root cause
    java.lang.NullPointerException
    	at com.beans.DerivationBean.getListDerivations(DerivationBean.java:30)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
    	at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    	at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    	at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    	at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    	at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    	at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    	at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    	at javax.faces.component.UIData.getValue(UIData.java:732)
    	at org.primefaces.component.api.UIData.getDataModel(UIData.java:759)
    	at javax.faces.component.UIData.getRowCount(UIData.java:356)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:806)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:788)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:281)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:243)
    	at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85)
    	at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    	at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
    	at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    	at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    	at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    je ne vois pas pourquoi l'erreur retourne que la liste est vide alors que la table "derivation" dans la bd contient 2 lignes.
    merci à vous.
    Images attachées Images attachées  

  2. #2
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    ta variable doit etre null, essaies de l´afficher pour en etre sur.

    Eric

  3. #3
    Candidat au Club
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Novembre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2015
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Citation Envoyé par jeffray03 Voir le message
    salut,
    ta variable doit etre null, essaies de l´afficher pour en etre sur.

    Eric
    Bonjour Eric,
    je pense que vous avez raison, en rectifiant l'instantiation de la variable dans le "DerivationBean" comme suit : (je pense que c'est faux ce que j'ai fait !! )
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    @Autowired
    	private DerivationService derivationService = new DerivationService() {
    		@Override
    		public List<Derivation> findAll() {
    			// TODO Auto-generated method stub
    			return null;
    		}
    	};
    la liste est affichée sans résultat, aucune ligne retournée.

  4. #4
    Membre actif
    Homme Profil pro
    Développeur Java/JavaEE
    Inscrit en
    Août 2014
    Messages
    194
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java/JavaEE

    Informations forums :
    Inscription : Août 2014
    Messages : 194
    Points : 290
    Points
    290
    Par défaut
    Bonjour,

    Je ne sais pas si je me trompe mais je ne vois pas ou tu as chargé ton modèle "listDerivations" dans ton bean managé "DerivationBean" puisque tu le passe comme paramètre dans ton tableau.

    Il y'a trois solutions possible:
    1- Que tu charge ton modèle ("listDerivations" dans ton Bean managé) dans une méthode annotée par un @postContruct puisque tu travaille avec JSF(la manière la plus simple).
    2- Que tu l'injecte comme une dépendance puisque tu travaille avec spring.
    3- Que tu passe ta méthode getListDerivations() comme paramètre pour ton tableau dans page .xhtml.

  5. #5
    Candidat au Club
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Novembre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2015
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Citation Envoyé par Maine13 Voir le message
    Bonjour,

    Je ne sais pas si je me trompe mais je ne vois pas ou tu as chargé ton modèle "listDerivations" dans ton bean managé "DerivationBean" puisque tu le passe comme paramètre dans ton tableau.

    Il y'a trois solutions possible:
    1- Que tu charge ton modèle ("listDerivations" dans ton Bean managé) dans une méthode annotée par un @postContruct puisque tu travaille avec JSF(la manière la plus simple).
    2- Que tu l'injecte comme une dépendance puisque tu travaille avec spring.
    3- Que tu passe ta méthode getListDerivations() comme paramètre pour ton tableau dans page .xhtml.
    Bonjour,

    Effectivement j'ai essayé les 3 solutions, voici le code du bean managé :
    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
    package com.beans;
    import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import com.model.Derivation;
    import com.service.DerivationService;
     
    @ManagedBean(name="derivationBean")
    @Scope
    public class DerivationBean {
    	private List<Derivation> listDerivations = new ArrayList<Derivation>();
    	@Autowired
    	private DerivationService derivationService;
     
    	public List<Derivation> getListDerivations() {
    		listDerivations = derivationService.findAll();
    		return listDerivations;
    	}
    	public void setListDerivations(List<Derivation> listDerivations) {
    		this.listDerivations = listDerivations;
    	}
    	@PostConstruct
    	public List<Derivation> retournerListDerivations(){
    		return this.getListDerivations();
    	}
    }
    dans la page, voilà
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <p:dataTable value="#{derivationBean.retournerListDerivations()}" var="derivation">

  6. #6
    Membre actif
    Homme Profil pro
    Développeur Java/JavaEE
    Inscrit en
    Août 2014
    Messages
    194
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java/JavaEE

    Informations forums :
    Inscription : Août 2014
    Messages : 194
    Points : 290
    Points
    290
    Par défaut
    Essai de modifier comme suit
    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
     
    package com.beans;
    import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import com.model.Derivation;
    import com.service.DerivationService;
     
    @ManagedBean(name="derivationBean")
    @ViewScoped
    public class DerivationBean {
    	public List<Derivation> listDerivations;
    	@Autowired
    	private DerivationService derivationService;
     
    	public List<Derivation> getListDerivations() {
    		return listDerivations;
    	}
    	public void setListDerivations(List<Derivation> listDerivations) {
    		this.listDerivations = listDerivations;
    	}
    	@PostConstruct
    	public void init(){
    		listDerivations = new ArrayList<Derivation>();
                    listDerivations = derivationService.findAll();
    	}
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <p:dataTable value="#{derivationBean.listDerivations}" var="derivation">

  7. #7
    Candidat au Club
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Novembre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2015
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Citation Envoyé par Maine13 Voir le message
    Essai de modifier comme suit
    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
     
    package com.beans;
    import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import com.model.Derivation;
    import com.service.DerivationService;
     
    @ManagedBean(name="derivationBean")
    @ViewScoped
    public class DerivationBean {
    	public List<Derivation> listDerivations;
    	@Autowired
    	private DerivationService derivationService;
     
    	public List<Derivation> getListDerivations() {
    		return listDerivations;
    	}
    	public void setListDerivations(List<Derivation> listDerivations) {
    		this.listDerivations = listDerivations;
    	}
    	@PostConstruct
    	public void init(){
    		listDerivations = new ArrayList<Derivation>();
                    listDerivations = derivationService.findAll();
    	}
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <p:dataTable value="#{derivationBean.listDerivations}" var="derivation">
    j'ai essayé votre code mais j'ai eu l'erreur suivante avec toujours la liste (NullPointerException)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    GRAVE: Error Rendering View[/listderivations.xhtml]
    com.sun.faces.mgbean.ManagedBeanCreationException: Erreur lors de l’injection de ressources dans le bean géré «derivationBean»
    	at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:227)
    	at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103)
    	at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)

    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
    Caused by: com.sun.faces.spi.InjectionProviderException
    	at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:115)
    	at com.sun.faces.vendor.WebContainerInjectionProvider.invokePostConstruct(WebContainerInjectionProvider.java:95)
    	at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:221)
    	... 47 more
    Caused by: java.lang.reflect.InvocationTargetException
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:113)
    	... 49 more
    Caused by: java.lang.NullPointerException
    	at com.beans.DerivationBean.init(DerivationBean.java:43)
    	... 54 more

  8. #8
    Membre actif
    Homme Profil pro
    Développeur Java/JavaEE
    Inscrit en
    Août 2014
    Messages
    194
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java/JavaEE

    Informations forums :
    Inscription : Août 2014
    Messages : 194
    Points : 290
    Points
    290
    Par défaut
    Tu peux s'il te plait mettre lors de l'injection de ton service
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    @ManagedProperty("#{derivationService}")
    private DerivationService derivationService;

  9. #9
    Candidat au Club
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Novembre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2015
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Citation Envoyé par Maine13 Voir le message
    Tu peux s'il te plait mettre lors de l'injection de ton service
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    @ManagedProperty("#{derivationService}")
    private DerivationService derivationService;
    j'ai essayé, voici ce que j'ai comme erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    com.sun.faces.mgbean.ManagedBeanCreationException: Impossible de créer le bean géré «derivationBean».  Les problèmes suivants ont été détectés :
         - La propriété «derivationService» du bean géré «derivationBean» n’existe pas.
    est-ce qu'il se peut que la méthode findAll() ne permet pas de récupérer les données de la base de données ?

  10. #10
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    essaies de mettre a la place de
    ceci
    Eric

  11. #11
    Membre actif
    Homme Profil pro
    Développeur Java/JavaEE
    Inscrit en
    Août 2014
    Messages
    194
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java/JavaEE

    Informations forums :
    Inscription : Août 2014
    Messages : 194
    Points : 290
    Points
    290
    Par défaut
    Ton code semble correcte !!!!!
    L'instruction <context:component-scan base-package="..."/> permet d'injecter tous les bean disponible dans ton package
    Mais essai d'injecter ton bean dans ton application-context.xml comme suit
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    <bean id="derivationService" class="Path_de ton_service">
    </bean>
    dans ton managedBean
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    @managedBean
    @viewScoped// ou le scope dont tu as besoin
    public class...
    .....
    @ManagedProperty("#{derivationService}")
    DerivationService derivationService;
     
    public void getTestMethod(){
    derivationService.canDoAnyThingFromService();
    }
    Après tu peux soit chargé ton modèle depuis le postContruct ou de faire appel à n'importe quel méthode dans ta page.xhtml.

Discussions similaires

  1. Erreur "java.lang.NullPointerException" avec JSF
    Par opium1er dans le forum JSF
    Réponses: 6
    Dernier message: 15/07/2013, 18h16
  2. Erreur java.lang.NullPointerException avec boîte à liste
    Par Halo2550 dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 06/03/2013, 13h12
  3. Réponses: 32
    Dernier message: 20/04/2007, 21h56
  4. [Débutant] Erreur java.lang.NullPointerException
    Par Kevin12 dans le forum Struts 1
    Réponses: 2
    Dernier message: 12/02/2007, 15h48
  5. Probleme erreur java.lang.NullPointerException
    Par Tsukaasa dans le forum Langage
    Réponses: 4
    Dernier message: 25/05/2006, 18h19

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