title: Why a NullPointer Exception from adding to a Collection?
body: <p>I am doing some experimentation on why I'm getting a NullPointerException when I try to add an object to a List. Below is my code, and the StackTrace. Do you know why I'm getting a NullPointerException when I try to add an object to a List?</p>
  
  <p>This is my test class.</p>
  
  <pre><code>package com.greathouse.aggregationtest;
  
  import com.greathouse.aggregationtest.types.Composite;
  import com.greathouse.aggregationtest.types.InitializedComponent;
  import com.greathouse.aggregationtest.types.NullComponent;
  import com.greathouse.aggregationtest.types.NullContainingComponent;
  
  public class App 
  {
      public static void main( String[] args )
      {
          System.out.println( "Testing aggregation" );
  
          InitializedComponent initializedComponent1 = new InitializedComponent();
          initializedComponent1.setInitializedString("Test Initialization 1 Successful");
  
          InitializedComponent initializedComponent2 = new InitializedComponent();
          initializedComponent2.setInitializedString("Test Initialization 2 Successful");
  
          NullComponent nullComponent1 = null;
          NullComponent nullComponent2 = null;
  
          NullContainingComponent nulLContainingComponent1 = new NullContainingComponent();
          nulLContainingComponent1.setIntendedNullString(null);
  
          NullContainingComponent nullContainingComponent2 = new NullContainingComponent();
          nullContainingComponent2.setIntendedNullString(null);
  
          System.out.println(initializedComponent1.getInitializedString());
          System.out.println(initializedComponent2.getInitializedString());
          System.out.println(nullComponent1);
          System.out.println(nullComponent2);
          System.out.println(nulLContainingComponent1.getIntendedNullString());
          System.out.println(nullContainingComponent2.getIntendedNullString());
  
          Composite composite = new Composite();
          composite.addInitializedComponents(initializedComponent1);
          composite.addInitializedComponents(initializedComponent2);
          composite.addNullComponent(nullComponent1);
          composite.addNullComponent(nullComponent2);
          composite.addNullContainingComponent(nulLContainingComponent1);
          composite.addNullContainingComponent(nullContainingComponent2);
      }
  }
  </code></pre>
  
  <p>This is the Composite object. The component object are simple wrappers with accessors.</p>
  
  <pre><code>package com.greathouse.aggregationtest.types;
  
  import java.util.List;
  
  public class Composite {
      private List&lt;InitializedComponent&gt; initializedComponents;
      private List&lt;NullComponent&gt; nullcomponents;
      private List&lt;NullContainingComponent&gt; nullContainingComponents;
      /**
       * @return the initializedComponents
       */
      public List&lt;InitializedComponent&gt; getInitializedComponents() {
          return initializedComponents;
      }
      /**
       * @return the nullComponents
       */
      public List&lt;NullComponent&gt; getNullcomponents() {
          return nullcomponents;
      }
      /**
       * @return the nullContainingComponents
       */
      public List&lt;NullContainingComponent&gt; getNullContainingComponents() {
          return nullContainingComponents;
      }
      public void addInitializedComponents(InitializedComponent initializedComponent) {
      this.initializedComponents.add(initializedComponent);
      }
      public void addNullComponent(NullComponent nullcomponent) {
      this.nullcomponents.add(nullcomponent);
      }
      public void addNullContainingComponent(NullContainingComponent nullContainingComponent) {
      this.nullContainingComponents.add(nullContainingComponent);
      }
  }
  </code></pre>
  
  <p>The console output.</p>
  
  <pre><code>Testing aggregation
  Test Initialization 1 Successful
  Test Initialization 2 Successful
  null
  null
  null
  null
  Exception in thread "main" java.lang.NullPointerException
  at com.greathouse.aggregationtest.types.Composite.addInitializedComponents(Composite.java:28)
  at com.greathouse.aggregationtest.App.main(App.java:41)
  </code></pre>