mercredi 6 mai 2015

Error when creating a list of objects - NullPointerException

This should be something fairly simple but I can't figure out my error. First up I am trying to write a program which will take user input and add the input as an object to a list called aList. I have two classes one called Group and one called ListObject.

Here is the Group class code

public class Group
{

   public List<Object> aList;


   public Group()
   {
      super();

      List<Object> aList = new ArrayList();

   }
 public void addToList(Object aName)
   {
       aList.add(aName);     

   } 
}

Here is my ListObject class

public class ListObject
{

    private String name;
    public int value;

    /**
     * Constructor
     */
    public ListObject(String aName)
    {
       super();
       this.name = aName;
       this.value = -1;            
    }
}

I need the method in the Group class to take user input, create an object of that name and then add it to the list aList and have all objects in that list be assigned as value of -1 to begin with. For some reason I am being returned a NullPopinterException. Hopefully you can point out what I've missed. Please note I did have this working when I was just adding strings instead of instances of the ListObject Objects.

Aucun commentaire:

Enregistrer un commentaire