Java Instantiating Array of Class in class space yields NullPointerException
<p>I'm new to Java (coming from C# among other languages) and if I create a variable and instantiate it in the empty space of a class like so;</p>

<pre><code>private SomeClass[][] someClassArray = new SomeClass[5][5];
</code></pre>

<p>I then receive a NullPointerException. However if I don't instantiate there but rather in a method it works fine. It seems like other times I can get away with this.</p>

<p>I tried looking this up in docs but didn't have any luck. Can someone provide some feedback or links to explain this so I understand how to properly instantiate in Java?</p>

<p><strong>Edit:</strong></p>

<p>In this instance it is error'ing when using <code>someClassArray.length</code> in an if statement. Why would this work correctly when instantiating in a method but not in the class.</p>

<p>The following is another example in which I get the NullPointerException when assigning a value...</p>

<pre><code>public class LevelClass extends SomeOtherClass{

    private Map&lt;String, LevelItemKeyClass&gt; levelItemKeys = new HashMap&lt;String, LevelItemKeyClass&gt;();

    private void CreateKeys() {
        levelItemKeys.put(new LevelItemKeyClass("abc")); //NullPointerException occurs here
    }
}
</code></pre>

<p>In the same manner as the above case, it works fine when instantiated in a method but not the class. The below works as expected;</p>

<pre><code>public class LevelClass extends SomeOtherClass{

    private Map&lt;String, LevelItemKeyClass&gt; levelItemKeys;

    private void CreateKeys() {
        levelItemKeys = new HashMap&lt;String, LevelItemKeyClass&gt;();

        levelItemKeys.put(new LevelItemKeyClass("abc"));
    }
}
</code></pre>