Java -NullPointerException thrown and I'm not sure why : <p>I am writing a program, in which, I have a method called "equals" to test whether two 2-dimensional arrays are "equal" to each other. equals returns whether the two args are equivalent in the sense that
    If both args are null, return true
    If one arg is null and the other isn't, return false;
    If the 2 arrays have a different number of rows, return false
else, return whether each element in arr1 is equivalent to the corresponding element in arr2 in the sense that both elements are null, or both elements refer to arrays with the same number of ints with the same values in the same order.</p>

<p>equals method definition:</p>

<pre><code>public static boolean equals( int[][] arr1, int[][] arr2){
    //both are null references
    if(arr1 == null &amp;&amp; arr2 == null)
        return true;
    //only one is a null reference
    if ((arr1 == null &amp;&amp; arr2 != null) || (arr1 == null &amp;&amp; arr2 != null))
        return false;

    //number of rows not identical
    if( arr1.length != arr2.length )
        return false;

    for(int i = 0 ; i &lt; arr1.length; i++){ //checking row equivalence 
        if(arr1[i] == null &amp;&amp; arr2[i]==null){ //both null
            continue;
        }
        if(arr1[i] == null || arr2[i] == null) //one is null
            return false;
        else if(arr1[i].length != arr2[i].length){
            return false;
        }
        else{
            for(int j = 0; j &lt; arr1[i].length; i++){ //#of columns equal, compare them
                System.out.println(i + "," + j); //helps debug
                if(arr1[i][j] != arr2[i][j])
                    return false;
            }
        }
    }
    return true;

} //equals
</code></pre>

<p>When I call </p>

<pre><code>equals( new int[][]{{1,2},{3,4},null}, new int[][]{{1,2},null,{3,4}} ) 
</code></pre>

<p>my program crashes, throwing the NullPointerException.</p>

<p>Eclipse says that it failed at this line:</p>

<pre><code>if(arr1[i][j] != arr2[i][j])
</code></pre>

<p>The problem is, as far as I can tell, my program shouldn't be entering the enclosing for-loop where this line is located to begin with. It should return false at this point:</p>

<pre><code>if(arr1[i] == null || arr2[i] == null) //one is null
            return false;
</code></pre>

<p>What am I doing wrong here? Thank you.</p>
