title: Printing 3D Array
body: <p>I am trying to print arrays before and after they are filled with user given inputs. Therefore, this would be the output if the given size is 3:</p>
  
  <blockquote>
    <p>//user inputs values for first matrix</p>
    
    <p>Before adding:</p>
    
    <p>Array [0] = </p>
    
    <p>Array [1] = </p>
    
    <p>Array [2] =   </p>
    
    <p>//add user inputs  </p>
    
    <p>After adding:</p>
    
    <p>Array [0] =   row 0 = [...]</p>
  
  <pre><code>            row 1 = [...] 
  
             ... depending on user given size of the first matrix
  </code></pre>
    
    <p>Array[1] =     Array[2] = </p>
    
    <p>//ask for inputs for second matrix</p>
    
    <p>Before Adding</p>
    
    <p>Array [0] =   row 0 = [...]</p>
  
  <pre><code>            row 1 = [...] 
  
             ... depending on user given size of the first matrix
  </code></pre>
    
    <p>Array[1] =     Array[2] = </p>
    
    <p>After adding</p>
    
    <p>Array [0] =   row 0 = [...]</p>
  
  <pre><code>          row 1 = [...] 
  
            ... depending on user given size of the first matrix
  </code></pre>
    
    <p>Array[1] = row 0 = [...]</p>
  
  <pre><code>          row 1 = [...] 
  
            ... depending on user given size of the first matrix
     Array[2] =
  </code></pre>
  </blockquote>
  
  <p>I wrote the following code to achieve this (In a class called ThreeDRayRunner):</p>
  
  <pre><code>public static void print(int [][][] array)
  {
      for (int i=0; i&lt;array.length; i++ )
      {
          for (int x=0; x&lt;array[i].length;x++)
          {
              System.out.println();
              System.out.print("row "+ x);
              for (int j=0; j&lt;array[i][x].length;j++)
              {
                      System.out.print (array[i][x][j]+ " ");
              }
          }
      }
  </code></pre>
  
  <p>However,I get the following error:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.NullPointerException
        at ThreeDRay.print(ThreeDRay.java:13)
        at ThreeDRayRunner.main(ThreeDRayRunner.java:41)</p>
  </blockquote>
  
  <p>Also, in the runner I ask the user to input the integer that will be stored in the 3D Array. To do this I ask for the size and print the 3D Array when is empty and after the given numbers are added. This is my code for that:</p>
  
  <pre><code>Scanner keyboard = new Scanner(in);
  
      out.print("How many matrices are you going to enter? ");
      int s = keyboard.nextInt();
  
  
      int[][][] d3= new int [s][][];
  
      for(int i = 0; i &lt; S; i++)
      {
          out.print("What is the size of the matrix " + i + " ? ");
          int size = keyboard.nextInt();
  
          int[][] mat = new int[size][size];
          out.println();
  
          for(int r=0; r&lt;mat.length; r++)
          {
              for(int c=0; c&lt;mat[r].length; c++)
              {
                  out.print("Enter a value for spot " + r + " - " + c );
                 mat[r][c]=keyboard.nextInt();
              }
          }
  
         out.println("The array before setting mat at spot "+i);
  
  
         ThreeDRay.print(d3);
  
         d3[i] = mat;
  
  
          out.println("The array after setting mat at spot "+i);
  
         ThreeDRay.print(d3);
  </code></pre>