i have a method to remove a student from a array of Students. this is what i have so far but does not seem to work.
public Student[] removeStudent(Student s) throws Exception{
boolean found = false;
for(int i = 0; i < nrStudents(this); i++){
if(students[i].equals(s)){
students[i] = null;
found = true;
break;
}
}
if (found == true){
return compact(students);
}
else
throw new Exception("Student Not Found.");
}
private Student[] compact(Student[] arr){
ArrayList<Student> list = new ArrayList<Student>();
for (Student s : arr){
if (!s.equals(null))
list.add(s);
}
arr = list.toArray(new Student[list.size()]);
return arr;
}
When i have 2 or more students in the array i get a NullPointerException. How can i remove a student from that array?