当我在2nd for循环中使用subject.getSubjectHasProgram()设置接口时,我得到了NullPointerException。但subject.getSubjectHasProgram()。size()显示为2,这是我想要的真实!! !!
Set<Subject> sub=(Set<Subject>)this.teacher.getSubjects();
for (Iterator<Subject> iterator = sub.iterator(); iterator.hasNext();) {
Subject next = (Subject)iterator.next();
if(next.getID()==this.ID){
this.Name=next.getName();
System.out.println("Subject Name="+this.Name);
this.CourseCode=next.getCourseCode();
this.CreditHour=next.getCreditHour();
**System.out.println("programscount="+next.getSubjectHasPrograms().size());**
for (Iterator iterator2 = next.getSubjectHasPrograms().iterator(); iterator2.hasNext();){
ProgramHasSubject programHasSubject = (ProgramHasSubject) iterator2.next();
this.selectedPrograms.add(programHasSubject.getProgram_ID());
System.out.println("Program ID : " + programHasSubject.getProgram_ID().getID());
System.out.println("Program Name : " + programHasSubject.getProgram_ID().getName());
System.out.println("Semester : " + programHasSubject.getSemester_ID());
}
break;
}
}
答案 0 :(得分:0)
在for循环中,你实际上有两个语句。
Iterator iterator2 = next.getSubjectHasPrograms().iterator();
and
iterator2.hasNext();
您是否确认next.getSubjectHasPrograms().iterator();
未返回null
?