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

Spring Boot Java Discussion :

Cannot invoke "fr.dawan.test3.service.ClientService.recupererClients()" because "this.clientService" is null


Sujet :

Spring Boot Java

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2011
    Messages
    431
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 431
    Points : 172
    Points
    172
    Par défaut Cannot invoke "fr.dawan.test3.service.ClientService.recupererClients()" because "this.clientService" is null
    Bonjour,

    J'ai l'erreur qui est dans le titre mais après de nombreuses recherche je n'arrive pas à régler ce problème voici mon code :

    Arborescence du projet :

    Nom : arborescence projet.png
Affichages : 93
Taille : 18,5 Ko

    Dans le package business :


    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
    package fr.dawan.test3.business;
     
    import java.io.Serializable;
    import javax.persistence.*;
     
     
    /**
     * The persistent class for the client database table.
     * 
     */
    @Entity
    @Table(name="client")
    public class Client implements Serializable {
    	private static final long serialVersionUID = 1L;
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.IDENTITY)
    	private int id_Client;
     
    	private String nom;
     
    	public Client() {
    		super();
    	}
     
    	public Client(String nom) {
    		super();
    		this.nom = nom;
    	}
     
    	public int getId_Client() {
    		return this.id_Client;
    	}
     
    	public void setId_Client(int id_Client) {
    		this.id_Client = id_Client;
    	}
     
    	public String getNom() {
    		return this.nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
     
    	@Override
    	public String toString() {
    		return "Client [id_Client=" + id_Client + ", nom=" + nom + "]";
    	}
     
    }
    Dans le controller :

    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 fr.dawan.test3.controller;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.servlet.ModelAndView;
     
    import fr.dawan.test3.service.ClientService;
     
    @Controller
    public class ClientController {
     
    	private ClientService clientService;
     
    	public ClientController() {
    		super();
    	}
     
        public ClientController(ClientService clientService) {
    		super();
    		this.clientService = clientService;
    	}
     
    	@GetMapping({ "/index", "/" })
        public ModelAndView accueil(){
        	// On déclare un ojb de type ModeleAndView et on l'instancie.
            ModelAndView mav = new ModelAndView();
            // On renseigne la vue associé au ModelAndView.
            // La vue est index.jsp, grâce au fichier application.properties à la ligne 13 il on est pas obligé
            // de préciser l'extension .jsp.
            mav.setViewName("index");
            // On ajoute à l'objet mav l'objet récupéré dans la bdd.
            System.out.println("clientService : "+clientService+"." );
            mav.addObject("clients", clientService.recupererClients());
            // On renvoi l'objet ModelAndView.
            return mav;
        }
    }
    Dans le dao :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package fr.dawan.test3.dao;
     
    import org.springframework.data.jpa.repository.JpaRepository;
     
    import org.springframework.data.repository.NoRepositoryBean;
     
    import fr.dawan.test3.business.Client;
     
    @NoRepositoryBean
    public interface ClientDao extends JpaRepository<Client, Integer> {
     
    }
    Dans le service :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package fr.dawan.test3.service;
     
    import java.util.List;
     
    import fr.dawan.test3.business.Client;
     
    public interface ClientService {
    	List<Client> recupererClients();
    }
    Et dans le service implémente :

    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 fr.dawan.test3.service.impl;
     
    import java.util.List;
     
    import org.springframework.stereotype.Service;
     
    import fr.dawan.test3.business.Client;
    import fr.dawan.test3.dao.ClientDao;
    import fr.dawan.test3.service.ClientService;
     
    @Service
    public class ClientServiceImpl implements ClientService {
     
    	private ClientDao clientDao;
     
    	public ClientServiceImpl() {
    		super();
    	}
     
    	public ClientServiceImpl(ClientDao clientDao) {
    		super();
    		this.clientDao = clientDao;
    	}
     
    	@Override
    	public List<Client> recupererClients(){
    		return clientDao.findAll();
    	}
     
    }
    Dans index.jsp :

    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
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
     
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
         <div>
             <c:forEach items="${ clients }" var="client">
                <p>${ client.id_Client }</p>
                <p>${ client.nom }</p>
             </c:forEach>
          </div>
    </body>
    </html>
    Si vous avez une idée merci d'avance.

  2. #2
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 937
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 937
    Points : 4 358
    Points
    4 358
    Par défaut
    Dans ClientController, ClientService n'est pas injecté car il n'y a pas d'annotation ni sur le champ ni sur le constructeur.
    Ajoutez @Autowired sur le constructeur devrait régler le problème.

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2011
    Messages
    431
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 431
    Points : 172
    Points
    172
    Par défaut
    D'accord merci pour ta réponse.

Discussions similaires

  1. Cannot invoke contains (char) on the array type char[]
    Par Jaymay054 dans le forum Général Java
    Réponses: 2
    Dernier message: 07/09/2016, 11h00
  2. [Débutant] Cannot invoke a non-delegate type
    Par Ashfor76 dans le forum C#
    Réponses: 6
    Dernier message: 27/08/2015, 11h51
  3. Erreur "cannot create windows service for mysql" à l'installation
    Par nicolas2603 dans le forum Installation
    Réponses: 3
    Dernier message: 28/02/2009, 15h05
  4. Cannot find service endpoint
    Par maysam dans le forum Services Web
    Réponses: 0
    Dernier message: 29/08/2008, 15h53
  5. [login] Cannot attach to services manager
    Par in dans le forum Installation
    Réponses: 7
    Dernier message: 10/03/2004, 15h27

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