I was in need of a dynamic “database” of objects and after some research, decided to use ArrayList. However, I cannot modify the arraylist with the code as follows:
public static ArrayList cprofiles;
...
cprofiles = new ArrayList();
...
...
Customer newc = new Customer (lna, fna, sinum, year, month, day);
cprofiles.add (newc);
After this declaration, I am trying to call to methods within the object using the following format cprofiles.get(0).getName() but I am getting an error stating
cannot find symbol (pointing to .getName())
when I try to compile the program. I have spent about an hour researching the proper method to modify this in an ArrayList but the sources I have found seem to suggest that I what I am doing is indeed correct.
Please aid me spotting my error and how I may fix it.
Thanks!
You should use the generic version of ArrayList, and not the raw version:
If you just use
ArrayList, the compiler doesn’t know what your list contains, so everything is considered as anObject. And you thus need to cast the returned object to its actual type: