java中的java.lang.NullPointerexception

时间:2015-03-16 16:59:03

标签: java error-handling exception-handling nullpointerexception

当我在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;
       }

   }

1 个答案:

答案 0 :(得分:0)

在for循环中,你实际上有两个语句。

Iterator iterator2 = next.getSubjectHasPrograms().iterator();
and
iterator2.hasNext();

您是否确认next.getSubjectHasPrograms().iterator();未返回null