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"));
}
}