0

I am working on a class for an Address Book Application on Java called "AddressBook.java", which I have written the following code:

package com.company;
import java.util.ArrayList;

class AddressBook {
    ArrayList<AddressEntry> addressEntryList = new ArrayList<AddressEntry>();
    AddressEntry test = new AddressEntry("john", "doe", "yes", "no", "maybe", 1, "I guess", "ok");
    
    addressEntryList.add(test);
}

Since I was just testing the test object, I had to give random values for the variables from my AddressEntry class and it seemed to work well. However, when I ran it in IntelliJ, I received the error

Cannot resolve symbol 'add'

This made the adding method not working.

1

2 Answers 2

4

You need to put it inside the main method.

Like this:

package com.company;
import java.util.ArrayList;

class AddressBook {
    public static void main(String[] args){
        ArrayList<AddressEntry> addressEntryList = new ArrayList<AddressEntry>();
        AddressEntry test = new AddressEntry("john", "doe", "yes", "no", "maybe", 1, "I guess", "ok");
        addressEntryList.add(test);
    }
}

Java cannot resolve method calls if the method call does not call from any method.

Sign up to request clarification or add additional context in comments.

2 Comments

First off, the original post contained some terminology that was of a sexual nature. Second, the whole "Hope that helps" is something the community has decided is unnecessary. Do not revert this post again.
0

You can only use your ArrayList object methods (for example .add, .remove etc) inside other methods, this can also be the main method (obviously)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.