Hello all, im rather confused about what's wrong with my code.... it says non-static method add(E) cannot be referenced from a static context, yet i can't see any reference to a static context....

here is my code; thanks in advance!

import java.util.*;
public class person {

public String name[];
public int phone[];
public String address[];
public String email[];
public person (String name, int phone, String address, String email)

String[] person = { "Josh", "Sam", "Paul", "Peter" };
ArrayList<String> al = new ArrayList<String>(Arrays.asList(person));

8 Years
Discussion Span
Last Post by javaAddict

Check the API and see if the add method is static or not:
Perhaps it should be al.add(person);


thanks for the suggestion, it partially cleared the problem.... however now it says:
Error: cannot find symbol
symbol : method add(java.lang.String[])
location: class java.util.ArrayList<java.lang.String>

and i don't really understand why it is saying this...


Because you're attempting to add the entire array. It doesn't work that way. Why don't you crack open the API documentation?

You're going to need to loop through that array and add each of the Strings separately. Or, crack open the API documentation and see if you can't figure out how to use a combination of an addAll call in ArrayList with an asList call from Arrays.


Because you have declared ArrayList to take Strings and you pass as parameter in the add() an array. Decide what you want, arrays or Strings and make the appropriate declaration

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.