Home > Net >  Method addFriend(Person) is undefined for the type ArrayList<Person>
Method addFriend(Person) is undefined for the type ArrayList<Person>

Time:11-16

I have 3 java source files.

  • Person.java
  • FriendsList.java
  • MyFriends.java

FriendsList contains an array list of objects of type Person and a method for adding a new friend to the list.

public class FriendsList{
    ArrayList<Person> friendsList = new ArrayList<Person>(100);
    // Constructor declaration of class
    public FriendsList(ArrayList<Person> friendsList){
            super();
            this.friendsList = friendsList;
    }    
    ...
    public void addFriend(Person friend){
            friendsList.add(friend);
    }
    ...
}

Now, in MyFriends.java I created a new array list of objects of type Person, I declared a new Person and I'm trying to add it to the list using the method addFriend(Person) but I'm getting an error "Method addFriend(Person) is undefined for the type ArrayList<Person>"

public class MyFriends{
    public void main(String[] args){
            ArrayList<Person> friendsList = new ArrayList<Person>(100);            
            Person f1 = new Person("Alice", "Anderson", "519-472-4910", "02", "19");
            friendsList.addFriend(f1);
    }
}

I tried all other methods defined in FriendsList.java but I get the same error message. I understand that if the source files are in the same folder, the syntax to use a method from the other file is objectName.method(a).

What am I doing wrong?

(Sorry if I'm not asking it right, it's my first question)

CodePudding user response:

I guess that you want to do the following instead:

public class MyFriends{
    public void main(String[] args){
        FriendsList friendsList = new FriendsList();            
        Person f1 = new Person("Alice", "Anderson", "519-472-4910", "02", "19");
        friendsList.addFriend(f1);
    }
}

You were instantiating an ArrayList<Person> but what you really want is to create an object of your own FriendsList class.


It seems that you don't have a no-args constructor in your FriendsList, so you need to do the following (instantiating a List of Person):

public class MyFriends{
    public void main(String[] args){
        FriendsList friendsList = new FriendsList(new ArrayList<Person>(100));            
        Person f1 = new Person("Alice", "Anderson", "519-472-4910", "02", "19");
        friendsList.addFriend(f1);
    }
}

Still, in my opinion, you should consider adding a no-args constructor to your FriendsList as follows so that my first suggestion would actually work:

public class FriendsList{
    ArrayList<Person> friendsList = new ArrayList<Person>(100);
    // Constructor declaration of class
    public FriendsList() {
    }  
    
    public FriendsList(ArrayList<Person> friendsList){
            super();
            this.friendsList = friendsList;
    }    
    ...
    public void addFriend(Person friend){
            friendsList.add(friend);
    }
    ...
}

CodePudding user response:

You are not actually using the FriendsList class. MyFriends should be:

public class MyFriends{
    public void main(String[] args){
            FriendsList friendsList = new FriendsList();            
            Person f1 = new Person("Alice", "Anderson", "519-472-4910", "02", "19");
            friendsList.addFriend(f1);
    }
}
  •  Tags:  
  • java
  • Related