Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Objects in an Array/Classes Help

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Objects in an Array/Classes Help

    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


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Objects in an Array/Classes Help

    What happens if user tries to make a deposit before creating an account?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects in an Array/Classes Help

    Quote Originally Posted by Junky View Post
    What happens if user tries to make a deposit before creating an account?
    Exception in thread "main" java.lang.NullPointerException
    at bankapp.BankApp.main(BankApp.java:90)
    Same error

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Objects in an Array/Classes Help

    You did not understand my point. If the Account has not been created how can you perform any functions (withdraw, deposit, etc)? You have to make sure Account objects are created and stored in your array before you can do anything with them.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects in an Array/Classes Help

    Quote Originally Posted by Junky View Post
    You did not understand my point. If the Account has not been created how can you perform any functions (withdraw, deposit, etc)? You have to make sure Account objects are created and stored in your array before you can do anything with them.
    I apologize. For the assignment it is assumed that the user chooses 1 or 2 first, creating an account.

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    When an account has been created and the user wants to deposit money, how does the program know which of the 50 Account array entrances that the money should be deposited to?

    Rolf

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Objects in an Array/Classes Help

    Quote Originally Posted by penguin0 View Post
    I apologize. For the assignment it is assumed that the user chooses 1 or 2 first, creating an account.
    But they are not being created first. That is why you are getting a NullPointerException. If no Account object is created and stored in the array the array will be full of nulls instead.
    Improving the world one idiot at a time!

Similar Threads

  1. Objects and Classes
    By Worst Programmer Ever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2013, 03:13 PM
  2. Objects and Classes
    By Kristenw17 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 23rd, 2013, 12:43 AM
  3. Polymorphorism with array objects of different classes?
    By EDale in forum Collections and Generics
    Replies: 7
    Last Post: April 14th, 2013, 03:54 PM
  4. Classes and Array of objects
    By dynasty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2013, 01:53 AM
  5. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 11:35 AM