title: Java arrays and scope
body: <p>I writing an application where i use an array within a class. This array is declared globally outside of any method scope so that all of the objects methods will be able to access the data. However, when i assign an object to a index that object is only available as a part of the array in that particular method. Any other method trying to read that index of the array gets a nullpointerexception. </p>
  
  <p>Some code illustrating my usage: </p>
  
  <pre><code>Account[] accounts = new Account[8000];
  
  
  public void addAccount() {
  
  
      cli.printMessage("Welcome to the account add wizard.");
      String holdername = cli.getInput(entername);
      int accountnr = Integer.parseInt(cli.getInput(enternr));
      BigDecimal accbalance = new BigDecimal(cli.getInput(enterinitialbalance));
  
      accounts[accountnr] = new Account(holdername, accountnr, accbalance);
      String accountinfo=accounts[accountnr].number + " " + " " + accounts[accountnr].holder + " " + accounts[accountnr].balance;
      cli.printMessage(accountinfo);
  
  }
  </code></pre>
  
  <p>Adding the account works all the way, this do not when ran from the same instance of this class: </p>
  
  <pre><code>public void getAccountinfo() {
  
      int accountnr = Integer.parseInt(cli.getInput(getinfo));
      String accountinfo=accounts[accountnr].number + "" + "" + accounts[accountnr].holder + accounts[accountnr].balance;
      cli.printMessage(accountinfo);
  }
  </code></pre>
  
  <p>Is this due to scope or is it due to me confusing instance and class variables? Might it be something completely different? </p>
  
  <p>The error i get is a nullpointer exception at </p>
  
  <pre><code>        String accountinfo=accounts[accountnr].number + "" + "" + accounts[accountnr].holder + accounts[accountnr].balance;
  </code></pre>
  
  <p>Grateful for any help! </p>