2017-10-18 28 views
-1

J'ai modèle avec paramètre d'objet.comment envoyer Liste <model> en ajoutant son paramètre en tant qu'objet

S'il y a 3 candidats différents, il devrait afficher 3 candidats, mais mon résultat est de répéter le dernier candidat 3 fois. Je ne reçois pas les 2 premiers détails du candidat.

public class CandidateFeedbackDisplay implements Serializable{ 
    private Candidate candidate; 
    private List<Integer> feedbackIds; 
//setter and getters 
} 




public List<CandidateFeedbackDisplay> list(Integer cID, Integer jID, String accepted) throws Exception { 
     Session session = this.sessionFactory.getCurrentSession(); 
     List<Candidate> candidateList = null; 
     CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay(); 
     List<CandidateFeedbackDisplay> feedbackDisplayList = new ArrayList(); 
//  List<CandidateFeedbackDisplay> feedbackDisplayListTest = null; 
     try { 
      Query query = session.createQuery("from Candidate WHERE phoneNumber IN (select DISTINCT mobileNo from InviteCandidates WHERE c_id= :cID AND j_id= :jID AND status= :accepted)");    
      query.setInteger("cID", cID);  
      query.setInteger("jID", jID); 
      query.setString("accepted", accepted);   
      candidateList = query.list(); 
      Iterator itr = candidateList.iterator(); 
      while(itr.hasNext()){ 
       Candidate candidate = (Candidate) itr.next(); 
       System.out.println("candidate.getCandidateID() : " + candidate.getCandidateID()); 
       List<CandidateFeedback> candidateFeedback = this.getFeedback(cID, jID, candidate.getCandidateID()); 
       Iterator itr1 = candidateFeedback.iterator(); 
       List<Integer> feedbackid = new ArrayList<Integer>(); 
       while(itr1.hasNext()){     
        CandidateFeedback Feedback = (CandidateFeedback) itr1.next(); 
        feedbackid.add(Feedback.getFeedbackID());    
       } 
       feedbackDisplay.setFeedbackIds(feedbackid); 
       feedbackDisplay.setCandidate(candidate);    
       feedbackDisplayList.add(feedbackDisplay); 
//   feedbackDisplayListTest.add(feedbackDisplay); // null pointer access error 
      } 
      }catch (Exception e) { 
       e.printStackTrace(); 
       this.logger.error("Error while fetching List :" + e); 
       return null; 
      } 
     return feedbackDisplayList; 
    } 
+0

Vous devez d'abord expliquer votre code (mettre un commentaire) et indiquer le code exact où vous avez problème et également supprimer le code inutile – Ravi

+0

merci pour votre réponse rapide – Priya

Répondre

1

Vous ajoutez le même objet à la liste trois fois. Vous devez créer un nouvel objet à chaque fois en déplaçant cette ligne:

CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay(); 

dans la boucle while, sinon vous continuez à modifier les propriétés des objets que vous a déjà posée. En fait, vous changez le même objet et l'ajoutez à la liste trois fois.

public List<CandidateFeedbackDisplay> list(Integer cID, Integer jID, String accepted) throws Exception { 
    ... 
    // DELETE HERE 
    // CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay(); 
    List<CandidateFeedbackDisplay> feedbackDisplayList = new ArrayList(); 
    try { 
     ... 
     while(itr.hasNext()) { 
      ... 
      // INSERT HERE 
      CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay(); 
      feedbackDisplay.setFeedbackIds(feedbackid); 
      feedbackDisplay.setCandidate(candidate);    
      feedbackDisplayList.add(feedbackDisplay); 
     } 
    } catch (Exception e) { 
     ... 
    } 
    return feedbackDisplayList; 
} 

Note complémentaire: Afin d'éviter une telle erreur la prochaine fois, vous pouvez changer votre objet paramètre CandidateFeedbackDisplay setters à une implémentation basée constructeur:

public class CandidateFeedbackDisplay { 
    private final Candidate candidate; 
    private final List<Integer> feedbackIds; 

    public CandidateFeedbackDisplay(Candidate candidate, List<Integer> feedbackIds) { 
     this.candidate = candidate; 
     this.feedbackIds = feedbackIds; 
    } 

    // no setters 
    // add getters or make fields public, but keep final 
} 

De cette façon, vous montrez vraiment que cet objet est juste un détenteur de valeur immuable. Vous ne pouvez pas refaire la même erreur et le constructeur peut raccourcir un peu le code. Les avantages et les inconvénients dépendent de votre cas spécifique, bien sûr.

+0

Merci. tu as sauvé ma journée. – Priya

+0

@Priya sûr, heureux d'entendre. J'ai ajouté une petite note à la fin qui peut aider à prévenir une telle erreur dans le futur. –

1

Mettre CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay(); intérieur en boucle parce que vous essayez de préparer Liste des CandidateFeedbackDisplay

S'il vous plaît trouverez ci-dessous le code, espérons qu'il vous aidera.

while(itr.hasNext()){ 
     CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay(); 
     Candidate candidate = (Candidate) itr.next(); 
     System.out.println("candidate.getCandidateID() : " + candidate.getCandidateID()); 
     List<CandidateFeedback> candidateFeedback = this.getFeedback(cID, jID, candidate.getCandidateID()); 
     Iterator itr1 = candidateFeedback.iterator(); 
     List<Integer> feedbackid = new ArrayList<Integer>(); 
     while(itr1.hasNext()){ 
      CandidateFeedback Feedback = (CandidateFeedback) itr1.next(); 
      feedbackid.add(Feedback.getFeedbackID()); 
     } 
     feedbackDisplay.setFeedbackIds(feedbackid); 
     feedbackDisplay.setCandidate(candidate); 
     feedbackDisplayList.add(feedbackDisplay); 
    } 
+0

Merci pour votre réponse – Priya

+0

welcome @Priya :) –