title: How to make an array from a class defined inside another class
body: <p>I am a novice Java programmer trying to use classes defined in a different file. So, I've written these two .java files:</p>
  
  <p><strong>First, there's MyLibrary.java:</strong></p>
  
  <pre><code>package mymainprogram;
  
  public class MyLibrary {
      public class MyRecord {
          int number;
          char letter;
      }
  
      public static int TriplePlusThree(int input_number) {            
          return ((input_number*3) + 3);
      }
  }
  </code></pre>
  
  <p><strong>Then, MyMainProgram.java:</strong></p>
  
  <pre><code>package mymainprogram;
  
  import java.util.Scanner;
  
  public class MyMainProgram {
      public static void main(String[] args) {
          Scanner keyread = new Scanner(System.in);
  
          System.out.print("Enter Number to Process: ");
          int num = keyread.nextInt();  
          int result = MyLibrary.TriplePlusThree(num);
          System.out.println("3x + 3 = "+result);
  
          String letters = "ABCDEFGHIJ";
          MyLibrary.MyRecord[] TenRecs = new MyLibrary.MyRecord[10];
  
          for (int i = 0; i &lt; 10; i++) {
              TenRecs[i].number = i;      //NullPointerException here
              TenRecs[i].letter = letters.charAt(i);           
          }
      }
  }
  </code></pre>
  
  <p>I had no problem getting the method to work just fine; now my goal is to create an array where each member of the array has an integer and character. (Note: I'm not looking for better ways to accomplish this objective; I'm merely using this trivial example to try to get this working). </p>
  
  <p>When I tried to run my program, I got:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException</p>
  </blockquote>
  
  <p>I researched this, and found <a href="http://www.javawithus.com/tutorial/array-of-objects" rel="nofollow noreferrer">this page</a>, which says:</p>
  
  <blockquote>
    <p>If we try to access the objects even before creating them, run time errors would occur. For instance, the following statement throws a NullPointerException during runtime which indicates that [this array] isn't yet pointing to [an] object. The objects have to be instantiated using the constructor of the class and their references should be assigned to the array elements in the following way.</p>
  </blockquote>
  
  <pre><code>studentArray[0] = new Student();
  </code></pre>
  
  <p>So, I tried to do that in my Main Program:</p>
  
  <pre><code>MyRecordArray[0] = new MyLibrary.MyRecord();
  </code></pre>
  
  <p>but that gives this error:</p>
  
  <blockquote>
    <p>an enclosing instance that contains MyLibrary.MyRecord is required</p>
  </blockquote>
  
  <p>That error message led me to <a href="https://stackoverflow.com/questions/7619505/how-can-i-resolve-an-enclosing-instance-that-contains-x-y-is-required">this Stack Exchange question</a>, which says:</p>
  
  <blockquote>
    <p>you have to create an object of X class (outer class) and then use objX.new InnerClass() syntax to create an object of Y class.</p>
  </blockquote>
  
  <pre><code>X x   = new X();
  X.Y y = x.new Y();
  </code></pre>
  
  <hr>
  
  <p>So, in accordance with that answer, I've added these two lines to my program:</p>
  
  <pre><code>MyLibrary mylibrary         = new MyLibrary();
  MyLibrary.MyRecord myrecord = mylibrary.new MyRecord();
  </code></pre>
  
  <p>Those lines don't give any warnings or compilation errors, so I feel like I'm one step closer, but I'm still trying to figure out how to make an array. I know if I wanted to make an array of integers, I would simply do this:</p>
  
  <pre><code>int[] TenInts = new int[10];
  </code></pre>
  
  <p>So, I've tried things like:</p>
  
  <pre><code>myrecord[] TenRecs = new myrecord[10];
  MyRecord[] TenRecs = new MyRecord[10];
  </code></pre>
  
  <p>But nothing is working, and I feel like I'm grasping at straws now. I get the feeling that the right set of eyes could solve this pretty quickly. </p>