Home » Java » java – How do I delete object from object array if its field variable = the parameter of the method-Exceptionshub

java – How do I delete object from object array if its field variable = the parameter of the method-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to delete an object instance called Entry if its surname field variable = the method parameter surname. This is what I have so far:

public class ArrayDirectory implements Directory {

    public Entry[] records_array = new Entry[100];
    List<Entry> record_list = new ArrayList<Entry>(Arrays.asList(records_array));
    public static void main(String[] args){
        ArrayDirectory newArrayDirectory = new ArrayDirectory();
        Entry a = new Entry("Alexander" , "H.A", "53454");
        Entry b = new Entry("Hughes","H.H", "12234" );
        Entry c = new Entry("Brown" , "L.B", "47665");
        Entry d = new Entry("Jenkins", "A.J", "34456");
        Entry e = new Entry("Cole", "P.C","57811");


        newArrayDirectory.insertEntry(a);
        newArrayDirectory.insertEntry(b);
        newArrayDirectory.insertEntry(c);
        newArrayDirectory.insertEntry(d);
        newArrayDirectory.insertEntry(e);

        newArrayDirectory.deleteEntryUsingName("Alexander");

    }



    public void deleteEntryUsingName(String surname) {
        // turns records array into ArrayList
        Predicate<Entry> condition = Entry -> Entry.getSurname().contains(surname);
        record_list.removeIf(condition);
        records_array = record_list.toArray(records_array);// turns record_list back into an array
        System.out.print(Arrays.toString(records_array));
    }

}

I keep getting a nullpointer exception in the main class where the method is called and I don’t really know what that means, to be honest.

Here is the Directory class:

import java.util.List;


public interface Directory {

    /**
     * Insert a new entry into the directory.
     *
     * @param entry the new entry to add
     */
    public void insertEntry(Entry entry);


    /**
     * Remove an entry from the directory using their surname.
     *
     * @param surname the surname of the entry to remove
     */
    public void deleteEntryUsingName(String surname);

    /**
     * Remove an entry from the directory using their extension number.
     *
     * @param number the extension number of the entry to remove
     */
    public void deleteEntryUsingExtension(String number);

    /**
     * Update an entry's extension number using their surname.
     *
     * @param surname   surname of the entry to be updated
     * @param newNumber the new number
     */
    public void updateExtensionUsingName(String surname, String newNumber);

    /**
     * Get the extension number of an entry using their surname.
     *
     * @param surname the surname of the entry
     * @return the extension number of the entry
     */
    public String lookupExtension(String surname);

    /**
     * Return an array list of all entries in the directory.
     *
     * @return an array list of all entries
     */
   public List<Entry> toArrayList();

}
How to&Answers:

The reason is that while declaring the record_list variable on the class you are converting the “records_array” array to list. This “records_array” array contains 100 NULL values initially and as a result, there are null Entry objects populated in the record_list. Then in deleteEntry method “getSurname()” is called on a null object. Hence the null pointer exception. You could write as below to avoid exception in the delete method:

Predicate<Entry> condition = entry -> Objects.nonNull(entry) && entry.getSurname().contains(surname);