title: Java: Accessing elements of a row in a multidimensional array?
body: <p>I am doing a Java project and have constructed a Polygon like this:</p>
  
  <pre><code>DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 0, 0},  new double[]{0, 0, 3, 3}, Color.red);
  </code></pre>
  
  <p>whereas the class DPolygon has the following constructor:</p>
  
  <pre><code>public DPolygon(double[] x, double[] y, double[] z, Color c)
  {
      Screen.NumberOf3DPolygons++;
      this.x = x;
      this.y = y;
      this.z = z;
      this.c = c;
      createPolygon();
  }
  </code></pre>
  
  <p>What I want to do is to calculate the sum of its z coordinates (which in this case will be = 6)
  Here is what I had in mind:</p>
  
  <pre><code>sum = DPolygons[NumberOf3DPolygons].z[0]+DPolygons[NumberOf3DPolygons].z[1]+
                      DPolygons[NumberOf3DPolygons].z[2]+DPolygons[NumberOf3DPolygons].z[3];
  </code></pre>
  
  <p>But it gives a <code>NullPointerException</code> because it doesn't recognize <code>DPolygons[NumberOf3DPolygons].z[0]</code> as the first z value of the polygon and so on.</p>
  
  <p>Can someone give me a clue what would be the right syntax for accessing each of these z elements? (Or how can I otherwise get that sum?)</p>