title: Inputting sets of numbers in java - Null pointer exception
body: <pre><code>import java.util.Scanner;
  import java.lang.Integer;
  public class points{
      private class Vertex{
          public int xcoord,ycoord;
          public Vertex right,left;
      }    
      public points(){
          Scanner input = new Scanner(System.in);
          int no_of_pts = Integer.parseInt(input.nextLine());
          Vertex[] polygon = new Vertex[no_of_pts];        
          for(int i=0;i&lt;no_of_pts;i++){
              String line = input.nextLine();
              String[] check = line.split(" ");           
              polygon[i].xcoord = Integer.parseInt(check[0]);
              polygon[i].ycoord = Integer.parseInt(check[1]);
          }    
      }    
      public static void main(String[] args){
          new points();    
      }    
  }
  </code></pre>
  
  <p>This is a very simple program in which I want to input n number of points into the system with their x and y co-ordinates</p>
  
  <pre><code>Sample Input :
  3
  1 2
  3 4
  5 6
  </code></pre>
  
  <p>However after entering "1 2" it throws a NullPointerException . I used Java debug to find the troubling line is</p>
  
  <pre><code>polygon[i].xcoord = Integer.parseInt(check[0]);
  </code></pre>
  
  <p>However the check variable correctly shows '1' and '2' . Whats going wrong ?</p>
  
  <p><strong>EDIT</strong> :
  Thanks to the answers, I realized I had to initialize each element of the array to a new object using</p>
  
  <pre><code>polygon[i] = new Vertex();
  </code></pre>