2017-10-18 49 views
-1

我有模型與對象參數。如何發送列表<model>通過添加其參數作爲對象

如果有3個不同的候選人,它應該顯示3個候選人,但我的輸出是重複最後一個候選人的細節3次。我沒有獲得前兩個候選人的詳細信息。

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

首先,你需要解釋一下你的代碼(認沽評論),並指出確切的代碼,你有問題,並且還去掉不必要的代碼 – Ravi

+0

感謝您的快速回復 – Priya

回答

1

您將相同的對象添加到列表三次。您可以通過移動這條線來創建新的對象,每次:

CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay(); 

進入while循環,否則你不斷改變你剛纔把對象的屬性。實際上,您正在更改同一個對象,並將其添加到列表中三次。

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; 
} 

附加說明:爲了防止下一次這樣的錯誤,你可以在你的參數對象CandidateFeedbackDisplay從setter方法改變爲基於構造函數實現:

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 
} 

這樣,你真的表明這個對象只是一個不變的值持有者。你不能再犯同樣的錯誤,構造函數可能會縮短代碼的一小部分。當然,好處和缺點取決於您的具體情況。

+0

謝謝。你救了我的一天。 – Priya

+0

@Priya當然,很高興聽到。我在最後添加了一個小記錄,可以幫助防止將來出現這樣的錯誤。 –

1

CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay();內部while循環,因爲您要準備CandidateFeedbackDisplay

列表請找到下面的代碼,希望這將有助於。

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

謝謝你的回覆 – Priya

+0

歡迎@Priya :) –