0

I have 3 classes, Main, Contacts, and ContactsManager. I'm getting the following error in my "searchContact" method. I don't know how to fix. I am looping through the array of contact indexes for as many contacts represented by the "friendsCount variable. Please help me fix the searchContact method. Thank you.

ContactsManager class

public class ContactsManager {

    // Fields
    Contact [] myFriends;
    int friendsCount;

    // Constructor
    ContactsManager() {
        this.friendsCount = 0;
        this.myFriends = new Contact[500];
    }

    // Methods
    void addContact(Contact contact) {
        myFriends[friendsCount] = contact;
        friendsCount++;
    }

    Contact searchContact(String searchName) {
        for(int i = 0; i < friendsCount; i++) {
            if(myFriends[i].name.equals((searchName))) {
                return myFriends[i];
            }
        }
        return  null;
    }
}

Contacts class

public class Contact {

    String name;
    String email;
    String phoneNumber;

}

Main class

public class Main {

    public static void main(String[] args) {
        ContactsManager myContactManager = new ContactsManager();

        Contact con1 = new Contact();
        con1.name = "Albert";
        con1.phoneNumber = "9545899977";

        myContactManager.addContact(con1);

        Contact con2 = new Contact();
        con1.name = "Jason";
        con1.phoneNumber = "9545899944";

        Contact con3 = new Contact();
        con1.name = "Larry";
        con1.phoneNumber = "9545899911";

        Contact con4 = new Contact();
        con1.name = "Mary";
        con1.phoneNumber = "9545899933";

        Contact con5 = new Contact();
        con1.name = "Saraht";
        con1.phoneNumber = "9545899900";

        myContactManager.addContact(con2);
        myContactManager.addContact(con3);
        myContactManager.addContact(con4);
        myContactManager.addContact(con5);

        System.out.println(myContactManager.searchContact("Jason"));
    }
}
jesse
  • 133
  • 1
  • 9
  • 1
    ***Copy/Paste Error:*** You only set `con1` fields. `con2.name` is never assigned, so `myFriends[1].name` is null. – Andreas Aug 12 '19 at 16:57

1 Answers1

4

You're always setting con1's values.

Your main method instantiates the othes (con2, con3 and con4) but doesn't set their name to anything (so it's null). The main method must change as follows.

    ContactsManager myContactManager = new ContactsManager();

    Contact con1 = new Contact();
    con1.name = "Albert";
    con1.phoneNumber = "9545899977";

    myContactManager.addContact(con1);

    Contact con2 = new Contact();
    con2.name = "Jason";
    con2.phoneNumber = "9545899944";

and so on.

Also, in your searchContact method, consider what happens if the contact isn't found (it returns null). Instead, consider throwing a custom not found exception, or instead of returning the Contact directly, return an Optional (since the search might not find anything).

geco17
  • 5,152
  • 3
  • 21
  • 38