0
Exception in thread "main" java.lang.NullPointerException
at Course.dropStudent(Course.java:95)
at CourseTester.main(CourseTester.java:59)

I'm writing a Course class that works with a Student class(that i'm not allowed to modify) and have been provided a CourseTester class to ensure everything works properly. I've been making my way through it but i noticed that i'm now getting that error. It's happening when i try to compare a Student id from the CourseTester class to the student id's already in the array. The entire program is over 300 lines so i'll just try to copy in the parts where the problem is.

Fist part is from the Course class that i am writing

public boolean dropStudent(Student student) {
     boolean dropped = false;
     int targetIndex = 0;
     Student[] newRoster = new Student[roster.length];

     for (int count = 0; count < roster.length; count++) {
         if (roster[count].getID() == (student.getID())) {//line95
             targetIndex = count;
             dropped = true;
             } 
         }

Second part is from the CourseTester that was provided.

// drop student not on roster or waitlist
    studentToDrop = studentsInSchool[14]; // WW
    dropped = course.dropStudent(studentToDrop);//line 59
    System.out.println("For " + studentToDrop.getName() + ", drop method should return false (student 
    is not enrolled): \t" + dropped);   
    System.out.println("\nCourse should still contain AA, DD, EE, FF, and GG on the course roster (in 
    any order) and HH, JJ, KK, and MM on the waitlist (in that exact order).");
    System.out.println("\n" + course + "\n");

Note, I have to use Arrays. The goal here is to find remove a student from the roster(drop from course). I need to do this by searching for his student ID number.

Tony Ruiz
  • 1
  • 1
  • `roster[count]` is `null`. – Robby Cornelissen Mar 02 '20 at 02:17
  • ... or `student` is `null`, which would be because `studentsInSchool[14]` is `null`. Of course, we don't know which, only you have to code. So start **debuging**! – Andreas Mar 02 '20 at 02:17
  • You should put print statements just before accessing `roster[count]` and `student` to see what their values are. Printing values is a basic tool for debugging. – WJS Mar 02 '20 at 02:19
  • I tried print them out. It didnt work. my best guess is i would need to print them from the CourseTester class seeing as the Course class that im writing doesnt have a main method – Tony Ruiz Mar 02 '20 at 02:24
  • embarrassingly i'm i dont know how to debug using the Eclipse tools. i usually just use print statements as WJS suggested but in this case i cannot – Tony Ruiz Mar 02 '20 at 02:26
  • @TonyRuiz You can actually use print statements to check nullity; insert the statement `System.out.println(roster[count]);` on line 94 as a comment above suggests – ajc2000 Mar 02 '20 at 02:46
  • @ajc2000 when i try that the program crashes at the line where i insert the print statement. I think the problem is roster[count] is null(i'm about sure it is) but... i dont know how to fix it – Tony Ruiz Mar 02 '20 at 02:52
  • i had to return false!! When the if statement failed it's caused the exception because it couldn't continue, so i added an else return false and that fixed it. Thank you guys for your help/time. – Tony Ruiz Mar 02 '20 at 02:59

0 Answers0