2017-10-18 48 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 :) –