+ Start a Discussion
Adriana Reyes 7Adriana Reyes 7 

List.add error

List<String> myList = new List<String>();
myList.add('a');

I'm just playing arround with lists and it seems basic, but I keep getting errors. What am I doing wrong? I literally just copied and pasted, and got the error. I get both errors on the second line:
- Extra ')', at 'a'.
- Invalid constructor name: myList.add
Raj VakatiRaj Vakati
Looks like you might have same variable myList  in your code some where else check  
OR 
YOu might have an apex class with name myList whihc is conflicting 

But this code is correct 

 
List<String> myList = new List<String>();
myList.add('a');

 
Adriana Reyes 7Adriana Reyes 7
public class TestListClass {
    List<String> brandNewColorsList = new List<String>();
    brandNewColorsList.add('blue');
}

This is my class. I'm still getting the same errors for the second line. I changed the name. Does it have something to do with the class?
Raj VakatiRaj Vakati
Looks like you have an apex class Name "List" .. Delete the List apex class and save it .. 

Go to Setup --> apex class --> you can find the class name "List"
Raj VakatiRaj Vakati
Is its solved ? Mark is solved 
Akshay_DhimanAkshay_Dhiman
Hi Adriana,

I have gone through your code and found that you have not created a constructor or method to add items to your list.
Have a look at the code below and try doing it yourself by initializing your list inside a method or constructor:
 
public class TestListClass {

    List<String> brandNewColorsList = new List<String>();

   public  TestListClass()
   {
   brandNewColorsList.add('blue');
    System.debug(brandNewColorsList);
   }

}


Hope this explanation will help you resolve your query. 
If you find it helpful mark it as best answer so other get help from this.

Thank you.
Akshay