I am trying to delete a field in an array.
The array contains objects of type Person (Person contains firstname, lastname, birthdate and ID).
My intention was to look up each array field and compare the input ID with all the array fields. When I find the right one, I will set it to null.
But I get:
Exception in thread "main" java.lang.NullPointerException
And I don't know why.
public static void removePerson(Person[] container) {
TextIO.putln("Enter ID of person to be removed");
int index = TextIO.getInt();
for ( int i = 0 ; i < container.length ; i ++) {
if (container[i].id == index)
container[i] = null;
}
}
container's elements arenull. We can't help with only the code you gave us, ascontainerisn't set up there. Please provide us with an SSCCE (Short, Self Contained, Correct (Compilable), Example).if (container[i].id == index)toif (container[i] != null && container[i].id == index).