title: Java Object Reference Array Initialization
body: <p>In java, how do object references in an array initialization work?</p>
  <p>For example, this works fine:</p>
  <pre><code>SomeObject object1;
  SomeObject object2;
  
  // ***
  
  object1 = new SomeObject();
  object2 = new SomeObject();
  
  SomeObject[] objects = {object1, object2};
  
  // processing objects
  </code></pre>
  <p>However, if I put the array initialization at the &quot;***&quot; mark, I later get a NullPointerException when processing the array elements. Why? The array contains object references already, why does it matter if I initialize the said objects before or after initializing the array?</p>