我有带对象参数的模型。
如果有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 :(得分: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
}
这样,你真的表明这个对象只是一个不可变的值持有者。你不能再犯同样的错误,构造函数可能会稍微缩短代码。当然,好处和缺点取决于您的具体情况。
答案 1 :(得分: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);
}