0

I am recieving this Error whenever I run my program :

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)

This is the Code that generates the exception which is implemented in my Course Class:

    public static void enroll(Student student){
        student.setStatus(true);
        enrollees.add(student);
    }

This is the Code from my AppCoreProcessor Class that calls the method:

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

    }

And finally this is the code that calls the enroll method from my AppCoreProcessor class:

   public void actionPerformed(ActionEvent event) {

        if(event.getSource()== enrollTo){
        AppCoreProcessor.enroll(modelRow,index);
        }

What I'm trying to here is that, I'm getting the selected index in my table which is exactly the same index in my ArrayList of students, and same way, getting the selected index of course from another table. I will now use these values to call the static method enroll(int,int) from my app processor class. and i just cant figure out why I'm getting the NullPointerException? please help me , i'm only new to java.

Edit* This is my implementation of my ArrayList of student and course,

    public class AppCoreProcessor {
private static ArrayList<Student> Student = ReadAndWrite.getDefaultStudentArrays();
private static ArrayList<Course> Course = ReadAndWrite.getDefaultCourseArrays();

and I'm using these Arrays as data in my JTables, And before using the enroll method, i made a System.out.println statement to display the Student at a given index and its really displaying the value, I checked the course is not null and the student is not null, but whenever i call the method of my Course Class enroll(Student s) to enroll the student in that course, it just throws nullpointer Exception ? which I dunno why ?

misserandety
  • 122
  • 1
  • 4
  • 13

2 Answers2

1

Either student is null OR enrollees is null. Make sure both of them are properly initialized or make a null check

public static void enroll(Student student){
    if(student != null && enrollees != null) {
       student.setStatus(true);
       enrollees.add(student);
    }
}
sanbhat
  • 17,522
  • 6
  • 48
  • 64
1

Questions I ask myself in such situations:

The stacktrace you posted says the NPE is fired at line 50. Is it the student.setStatus(true); line or the enrollees.add(student); line?

If Line 50 is the student.setStatus(true); line then the student parameter is null. This can happen if oldStudent.get(index) is null, that is: the list oldStudent contains, at position index a null value. You'll have to go to the code that pushes value into the list and check that it does not push nulls. Note that the oldStudent list itself is not null. If it were then the exception would have been thrown by this line oldCourse.get(modelRow).enroll(oldStudent.get(index));.

If Line 50 is the enrollees.add(student); line then you should check the place were the enrollees field is assigned and make sure it is not assigned with null. Here is is the opposite of the first case: it is not a value inside the list but the list itself.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118
  • Hooraah! , Yes you're right! , I haven't declare the Arraylist enrollees in my Course class thats why its NULL! , thank you so much for making me realize that. btw. line 50 is the enrollees.add(student) . – misserandety Jul 16 '13 at 05:39