title: How can I fix my NullPointerException error?
body: <p>I've been breaking my head over how to fix my code, but I haven't had any success. I'm trying to create a generic method that allows the user to specify what type of data the array will hold. Unfortunately, my methods don't work on the array, which causes it to be empty...which leads to the error. Here's the relevant code which includes the declared variables, constructor, addLast method, and relevant line numbers:</p>
  
  <pre><code>public class ArrayLinearList&lt;E&gt; implements LinearListADT&lt;E&gt; {
  
  private E[] list;
  private E[] temp;
  private int counter = 0;
  private int length = DEFAULT_MAX_CAPACITY;
  
  public ArrayLinearList() {
      list = (E[]) new Object [length];
  }
  
  public void addLast(E obj) {
      increaseSize();
      list[counter] = obj;
      counter++;
  }
  </code></pre>
  
  <p>Here is part of the tester program:</p>
  
  <pre><code>   public class P1Tester {
  
     private LinearListADT&lt;Integer&gt; list;
  
     public P1Tester() {
         list = new ArrayLinearList&lt;Integer&gt;();    
  16     runTests();
  17 }
  18    
  19 private void runTests() { 
  20     for(int i=1; i &lt;= 10; i++)
  21     list.addLast(i);         
  22     System.out.println("Should print 1 .. 10"); 
  23 for(int x : list)
         System.out.print(x + " ");
         System.out.println("\n");
     }
  </code></pre>
  
  <p>And finally, here's part of the interface I need to implement:</p>
  
  <pre><code>public interface LinearListADT&lt;E&gt; extends Iterable&lt;E&gt; {
  public static final int DEFAULT_MAX_CAPACITY = 100;
  
  //  Adds the Object obj to the end of list.  
  public void addLast(E obj);
  }
  </code></pre>
  
  <p>Whenever I run the tester, I get this error:</p>
  
  <pre><code>Should print 1 .. 10
  ERROR java.lang.NullPointerException
  java.lang.NullPointerException
  at data_structures.P1Tester.runTests(P1Tester.java:23)
  at data_structures.P1Tester.&lt;init&gt;(P1Tester.java:16)
  at data_structures.P1Tester.main(P1Tester.java:95)
  </code></pre>
  
  <p>Sorry if this is long but I really need the help!</p>
  
  <p>EDIT: Here is my code for iterator.</p>
  
  <pre><code>public Iterator&lt;E&gt; iterator() {
      return new Iterator&lt;E&gt;(){
  
          int iterIndex = 0;
  
          public boolean hasNext() {
          return iterIndex &lt;= counter;
          }
  
          public E next() {
              if (!hasNext())
              throw new NoSuchElementException();
              return (list[iterIndex++]);
              }
          public void remove(){
              throw new UnsupportedOperationException();
              }
          };
  
      }
  </code></pre>