Java 在我的类中传递/赋值时出现NullPointerException

Java 在我的类中传递/赋值时出现NullPointerException,java,Java,每当我运行程序时,我都会收到此错误: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Course.enroll(Course.java:50) at AppCoreProcessor.enroll(AppCoreProcessor.java:62) at CourseWindow.actionPerformed(CourseWindow.java:91) at javax.swing.Abstra

每当我运行程序时,我都会收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Course.enroll(Course.java:50)
at AppCoreProcessor.enroll(AppCoreProcessor.java:62)
at CourseWindow.actionPerformed(CourseWindow.java:91)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
这是生成异常的代码,该异常在我的课程类中实现:

    public static void enroll(Student student){
        student.setStatus(true);
        enrollees.add(student);
    }
这是我的AppCoreProcessor类中调用该方法的代码:

    public static void enroll(int modelRow, int index) {
    oldCourse.get(modelRow).enroll(oldStudent.get(index));

    }
最后,这是从我的AppCoreProcessor类调用enroll方法的代码:

   public void actionPerformed(ActionEvent event) {

        if(event.getSource()== enrollTo){
        AppCoreProcessor.enroll(modelRow,index);
        }
我想在这里做的是,我在我的表中得到所选的索引,它与我的学生数组列表中的索引完全相同,同样的方法,当然是从另一个表中得到所选的索引。现在,我将使用这些值从我的应用处理器类调用静态方法enroll(int,int)。我就是不明白为什么我会得到NullPointerException?请帮帮我,我才刚接触java

编辑*这是我对学生和课程的ArrayList的实现

    public class AppCoreProcessor {
private static ArrayList<Student> Student = ReadAndWrite.getDefaultStudentArrays();
private static ArrayList<Course> Course = ReadAndWrite.getDefaultCourseArrays();
公共类AppCoreProcessor{
private static ArrayList Student=ReadAndWrite.getdefaultstudentarray();
private static ArrayList Course=ReadAndWrite.getdefaultcourserrays();

我在JTables中使用这些数组作为数据,在使用enroll方法之前,我做了一个System.out.println语句,以显示给定索引处的学生,并显示其真正的值,我检查了课程不为null,学生不为null,但每当我调用课程类enroll(学生s)的方法时要让学生参加该课程,它只会抛出null指针异常?我不知道为什么?

要么
student
is
null
,要么
注册者
is
null
。确保两者都正确初始化或进行null检查

public static void enroll(Student student){
    if(student != null && enrollees != null) {
       student.setStatus(true);
       enrollees.add(student);
    }
}

在这种情况下,我会问自己:

你发布的stacktrace说NPE是在第50行触发的。是
student.setStatus(true);
行还是
Enrollements.add(student);

如果第50行是
student.setStatus(true);
行,则
student
参数为
null
。如果
oldstuent.get(index)出现这种情况
为空,即:列表
oldStudent
索引
位置包含一个
null
值。您必须转到将值推入列表的代码,并检查它是否未推入空值。请注意,
oldStudent
列表本身不是空值。如果是空值,则异常将由是行
oldCourse.get(modelRow).注册(oldstuent.get(index));


如果第50行是
注册者。添加(学生);
行然后你应该检查是否分配了
注册者
字段,并确保未分配
null
。第一种情况与此相反:它不是列表中的值,而是列表本身。

你回答了自己的问题:)。只需检查学生是否为null或注册者是否为null。请ee Hoorah!是的,你是对的!我还没有在我的课程中声明Arraylist报名者,这就是为什么它为空!非常感谢你让我意识到这一点。顺便说一句,第50行是报名者。添加(学生)。