title: Sum of matrix elements Java
body: <p>I'm writing a method with 2 parameters, one int matrix and 1 int array. The method  must return the sum of the elements of a[][] having a multiple in the array b[].</p>
  
  <p>Here is the method code</p>
  
  <pre><code>public static int sum(int[][] a, int[] b) {
      int matrixSum = 0;
  
      if (a != null &amp;&amp; b != null) {
          for (int i = 0; i &lt; a.length; i++) {
              for (int j = 0; j &lt; a.length; j++) {
                  boolean multiple = false;
                  boolean nullRow = false;
                  if (a[i] == null || a[i].length &lt; 1) {
                      nullRow= true;
                  } else {
                      for (int k = 0; k &lt; b.length &amp;&amp; !multiple; k++) {
                          if (b[k] % a[i][j] == 0) {
                              matrixSum += a[i][j];
                              multiple = true;
                          }
                      }
                  }
              }
          }
      }
      return matrixSum;
  }
  </code></pre>
  
  <p>This is the test class</p>
  
  <pre><code>public static void main(String[] args) {
      int[][] a = {{2, 3, 1}, {1, 1, 1}, {2, 2, 2}};
      int[][] a1 = {{2, 3}, {1, 1, 1}, {2, 2, 2}};
      int[][] a2 = {{2, 3, 4}, {1, 1}, {2, 2, 2}};
      int[][] a3 = {{2, 3, 4}, {1, 2, 3}, {1, 1}};
  
      int[] b = {7, 8, 7, 20};
  
  
      System.out.println(sum(a, b)); // 15
      System.out.println(sum(a1, b)); // 11
      System.out.println(sum(a2, b)); // 14
      System.out.println(sum(a3, b)); // 11
  
  }
  </code></pre>
  
  <p>In some cases it works in others no, some the result is incorrect and other times I receive OutBound error</p>