Hello, currently I am working on a programming assignment where I need to create and manipulate bank accounts with user entered information. I have to use objects in a separate class for the bank accounts to process all of the information. I pretty much have all of the code written out, I would say most of it makes sense, but I cannot get it to work Below is the main method and also the separate class file. The error I get pretty much no matter which option from the menu I choose is, "Exception in thread "main" java.lang.NullPointerException at bankapp.BankApp.main(X) where x is the line the error is on, so I know the error has to do with my accountArray[nCount], but I just have no idea how else I could write it or where I am going wrong. Thank you for any help, I know the code is quite long.
package bankapp; import java.util.Scanner; /** */ public class BankApp { /** * Displays menu and runs selected choice to perform several banking * applications and calculations. * @param args the command line arguments */ public static void main(String[] args) { //Declare variables Scanner input = new Scanner(System.in); //Reads user input int nSelection = 0; //Users menu choice Accounts [] accountArray = new Accounts [50]; //Array of accounts int nCount = 0; //Array increment double dMainBal = 0; //Starting balance double dMainRate = 0; //Interest rate double dMainChange = 0; //Deposit or withdrawal amount do { //Display Menu System.out.println("\n" + "ACCOUNT PROCESSING MENU\n" + "1. Create new account - empty account\n" + "2. Create new account - information available\n" + "3. Make a deposit\n" + "4. Make a withdrawal\n" + "5. Calculate monthly interest\n" + "6. View account balance\n" + "7. Next available account number\n" + "8. Update monthly interest rate\n" + "9. Print account information\n" + "10. Exit"); //Prompt user to make a menu selection System.out.println("Please make a section from the menu above: "); //Reads user's menu choice nSelection = input.nextInt(); if(nSelection == 1){ //Create new account in account array accountArray[nCount] = new Accounts(); //Print account number System.out.println("Account #" + accountArray[nCount].nAccountNumber + " successfully created."); //Increment nCount nCount++; }//end if else if(nSelection == 2){ //Prompt user for starting balance System.out.println("Please enter the starting balance: "); //Read starting balance dMainBal = input.nextDouble(); //Prompt user for starting interest rate System.out.println("Please enter the starting interest rate"); //Read starting interest rate dMainRate = input.nextDouble(); //Add Account to array, create number accountArray[nCount] = new Accounts(dMainBal, dMainRate); //Display account number System.out.println("Account #" + accountArray[nCount].nAccountNumber + " successfully created."); //Increment nCount nCount++; }//end else if else if(nSelection == 3){ //Prompt user for deposited amount System.out.println("Please enter the deposit amount: "); //Read user input dMainChange = input.nextDouble(); //Send deposited amount to object accountArray[nCount].setChange(dMainChange); //Call Deposit to change balance accountArray[nCount].Deposit(); }//end else if else if(nSelection == 4){ //Prompt user for deposited amount System.out.println("Please enter the withdrawal amount: "); //Read user input dMainChange = input.nextDouble(); //Send withdrawal amount to object accountArray[nCount].setChange(dMainChange); //Call Withdrawal to change balance accountArray[nCount].Withdrawal(); }//end else if else if(nSelection == 5){ //Print interest earned System.out.println("Monthly interest earned: " + accountArray[nCount].Interest());} else if(nSelection == 6){ //Print account balance System.out.println("Account balance: " + accountArray[nCount].dBalance);} else if(nSelection == 7){ //Print next available account number System.out.println("Next available account number: " + accountArray[nCount].nNextAccountNumber);} else if(nSelection == 8){ //Prompt user for new rate System.out.println("What is the new monthly interest rate? "); //Read user input of new interest rate dMainRate = input.nextDouble(); //Send new rate to object accountArray[nCount].setRate(dMainRate); }//end else if else if(nSelection == 9){ //Print account information System.out.println("Account number: " + accountArray[nCount].nAccountNumber + "\nAccount Balance: " + accountArray[nCount].dBalance + "\nMonthly Interest: " + accountArray[nCount].dInterestRate); }//end else if else if(nSelection == 10){ //Exit System.out.println("Exiting program.");} else //Display error for invalid selection System.out.println("Invalid selection, please" + " run the program again."); }while(nSelection != 10);//end do while loop }//end main }//end class * * To change this template, choose Tools | Templates * and open the template in the editor. */ package bankapp; /** */ public class Accounts { //Variables int nAccountNumber; //Account number int nNextAccountNumber = 123; //Next available account number double dBalance = 0; //Account balance double dInterestRate = 0; //Account interest rate double dChange = 0; //Deposited/withdrawal amount double dInterest = 0; //Total monthly interest //Default constructor public Accounts(){ nAccountNumber = nNextAccountNumber++; } //Overload constructor public Accounts(double dStartBal, double dStartRate){ dBalance = dStartBal; dInterestRate = dStartRate; } //sets interest rate public void setRate(double dRate2){ dInterestRate = dRate2; } //sets deposit/withdrawal amount public void setChange(double dChange2){ dChange=dChange2; } //calculates balance after deposit public void Deposit(){ dBalance = dBalance + dChange; } //calculates balance after withdrawal public void Withdrawal(){ //if insufficient funds, display message if(dChange>dBalance) System.out.println("Insufficient funds."); //else, calculate balance after withdrawal else dBalance = dBalance - dChange; } //calculates monthly interest public double Interest(){ dInterest = dInterestRate * dBalance; return dInterestRate; } }//ends class