title: Cant fix error Exception in thread "main" java.lang.NullPointerException
body: <p>I keep getting this error:</p>
  
  <p>Exception in thread "main" java.lang.NullPointerException</p>
  
  <p>at ButtonsLab.pvc1(ButtonsLab.java:110)</p>
  
  <p>at ButtonsLab.main(ButtonsLab.java:155)</p>
  
  <p>There are some people who have asked this question but I can't seem to find one that fixes this problem. I know it has something to do with the array in the pvc1() method.</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.ArrayList;
  import java.util.Random;
  
  class ButtonsLab
  {
     ArrayList&lt;Integer&gt; buttons;
  
  ButtonsLab(){
     buttons = new ArrayList&lt;Integer&gt;();
  }
  
    public void fillMyArray(){
       for(int i = 0; i &lt; 5; i++){
       buttons.add(i + 1);
    }
  }
  
  public static int pvp(int buttonNum){
  
    Scanner in = new Scanner(System.in);
    int player1 = 0;
    int player2 = 0;
    int x = 0; //determines who wins
  
    while (buttonNum &gt; 1){
  
       while (player1 &lt; 1 || player1 &gt; 3){
  
          System.out.println("Player 1: How many buttons do you take?(1-3)");
  
          player1 = in.nextInt();
  
       }
  
       buttonNum -= player1;
  
       if(buttonNum != 1){
          System.out.println("There are " + buttonNum + " Buttons on the board.");
       }
       else{
          System.out.println("... There is 1 Button on the board.");
       }
  
       if(buttonNum == 1){
  
          x = 1;
  
       }
  
       else{
          while (player2 &lt; 1 || player2 &gt; 3){
  
             System.out.println("Player 2: How many buttons do you take?(1-3)");
  
             player2 = in.nextInt();
  
          }
       }
  
       buttonNum -= player2;
  
       if(buttonNum != 1){
          System.out.println("There are " + buttonNum + " Buttons on the board.");
       }
       else if(x !=1){
          System.out.println("... There is 1 Button on the board.");
       }
  
       player1 = 0;
       player2 = 0;
  
    }
  
    if(x == 1){
       while (player2 &lt; 1 || player2 &gt; 3){
  
          System.out.println("Player 2: How many buttons do you take?(1-3)");
  
          player2 = in.nextInt();
  
       }
  
       System.out.println("Player 2, you lose.");
    }
    else{
       while (player1 &lt; 1 || player1 &gt; 3){
  
          System.out.println("Player 1: How many buttons do you take?(1-3)");
  
          player2 = in.nextInt();
  
       }
  
       System.out.println("Player 1, you lose.");
  
    }
    return x;
  }
  
  public static int pvc1(int buttonNum){
    Scanner in = new Scanner(System.in);
    int player1 = 0;
    int x = 0; //determines who wins
  
    ButtonsLab [] aiChoice;
    aiChoice = new ButtonsLab[buttonNum];
  
    for(int i = 0; i &lt; aiChoice.length; i++){
       aiChoice[i].fillMyArray();
    }
  
    while (buttonNum &gt; 1){
       while (player1 &lt; 1 || player1 &gt; 3){
  
          System.out.println("Player 1: How many buttons do you take?(1-3)");
  
          player1 = in.nextInt();
  
       }
  
       buttonNum -= player1;
  
       int rnd = new Random().nextInt(aiChoice.length);
       System.out.println("AI selects " + rnd);
  
       player1 = 0;
  
    }
    return x;
  }
  
  public static void main (String[] args)
  {
    Scanner in = new Scanner(System.in);
    int buttonNum = 0;
    int gameOpt = 0;
  
    while (buttonNum &lt; 10 || buttonNum &gt; 100){
  
       System.out.println("How many buttons are on the board initially?(10-100)");
  
       buttonNum = in.nextInt();
    }
  
    System.out.println("Options: Play against a friend (1) Play against easy computer (2) Play against hard computer (3)");
  
    gameOpt = in.nextInt();
  
    switch(gameOpt){
       case 1:
          pvp(buttonNum);
          break;
       case 2:
          pvc1(buttonNum);
          break;
  
  
    }
  
  }
  
  }
  </code></pre>