0

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?

Leon
  • 1,262
  • 3
  • 20
  • 41
  • Please post the full stack trace. – Rohit Jain Jan 21 '13 at 18:41
  • 3
    `if (!s.equals(null))` => `if (s != null)`. But instead of "nullifying" the Student to be removed and compacting the array, you could simply add all the students to an arraylist (except the one to be removed) and transform that arraylist to an array. – assylias Jan 21 '13 at 18:41
  • 1
    @assylias.. Post that as answer. – Rohit Jain Jan 21 '13 at 18:42
  • 4
    I strongly suggest you to use an `ArrayList` here, I don't see the point of using an array if you are going to remove elements by setting them to null to then convert it to a list and again into an array. – Jack Jan 21 '13 at 18:42
  • @RohitJain I'm more inclined to close as too localized to be honest. – assylias Jan 21 '13 at 18:44

4 Answers4

3

Don't use .equals() to check for null - to fix your code change the if(!s.equals(null)) line to a if (s != null).

Why?

Java null check why use == instead of .equals()

Using an ArrayList makes more sense for this problem. I suggest looking it up - there are several good usage examples and sources.

Community
  • 1
  • 1
PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
0

I am pretty sure you have to copy all the data except the null data to an entirely new array. Given this is a costly operation, I recommend ArrayLists http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

With an arraylist, you get the benefit of being able to reference by index, but you can also simply remove elements with:

if(node.data==null) {
     remove(node);
}
corvid
  • 10,733
  • 11
  • 61
  • 130
0

The following:

    if (!s.equals(null))

should read

    if (s != null)
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

This is what i did to fix it

private Student[] compact(Student[] arr){
    Student[] stud = new Student[arr.length];
    int count = 0;
    for(int i = 0; i < arr.length; i++){
        if(arr[i] != null){
            stud[count] = arr[i];
            count++;
        }
    }
    students = stud;
    return students;
}

i set the student to null first and the i use the compact helper method to compact the array so it has no null elements.

Leon
  • 1,262
  • 3
  • 20
  • 41