-1

I have created a program with three classes: Bank, Customer, Account in order to calculate the compound interest that a user inputs.

Every time I run the code, I receive errors in lines 76 and 82.

Line 76 is: private final double INTEREST_SAVING = customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);

Line 82 is: private final double INTEREST_CHEQUE = customerObj.getCheckAccount().getFinalBalance(0.0210,numYears);

These errors are as follows:

Exception in thread "main" java.lang.NullPointerException
    at Bank.<init>(Bank.java:76)
    at Bank.main(Bank.java:82)

I am running the program in eclipse IDE and there are no visual errors or warnings shown before I run the program.

Here is my code:

import java.util.Scanner;

class Account{

private double balance;

private final int NUM_COMPOUND_TIMES = 12;

Account(double Balance) { //constructor with parameter

    balance = Balance;
}

public double getInitialBalance(){ //get the initial balance method

    return balance;
}

public double getFinalBalance(double rate, int numYears) { //get the final balance method

    balance = getInitialBalance()*Math.pow((1+(rate/NUM_COMPOUND_TIMES)), 
(NUM_COMPOUND_TIMES*numYears));
    return balance;
    }
}



class Customer{

private String customerName;
private Account checkAccount;
private Account savingAccount;


Customer(String custName, Account savingAcc, Account checkAcc){ //constructor with 
parameters

    customerName = custName;

    savingAccount = savingAcc;

    checkAccount = checkAcc;

}

public Account getSavingAccount(){

    return savingAccount;
}

public Account getCheckAccount() {

    return checkAccount;
}

public String getCustomerName() {


    return customerName;
}
}



public class Bank {


private Customer customerObj;
private int numYears;
private final double INTEREST_SAVING = 
customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);
private final double INTEREST_CHEQUE = 
customerObj.getCheckAccount().getFinalBalance(0.0210,numYears);


public static void main(String[] args) {

    Bank bankObj = new Bank();

    bankObj.createCustomerAccounts();

    bankObj.printInitialBalances();

    bankObj.printFinalBalances();

}


public void createCustomerAccounts() {

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the customer's full name: ");
    String custName = keyboard.nextLine();
    System.out.println("Enter the number of years: ");
    numYears = keyboard.nextInt();
    System.out.println("Enter initial balance for the savings account: ");
    double ibSavings = keyboard.nextDouble();
    System.out.println("Enter initial balance for the cheque account: ");
    double ibCheck = keyboard.nextDouble();
    keyboard.close();

    Account savingAcc = new Account(ibSavings);
    Account checkAcc = new Account(ibCheck);
    customerObj = new Customer(custName, savingAcc, checkAcc);
}


public void printInitialBalances() {

    String name = customerObj.getCustomerName();
    double ibSav = customerObj.getSavingAccount().getInitialBalance();
    double ibChq = customerObj.getCheckAccount().getInitialBalance();
    String savRate = "2.95%";
    String checkRate = "2.10%";
    String dollar = "$";

    System.out.printf("========================================== %n");
    System.out.printf("Initial account balances %n");
    System.out.printf("========================================== %n");
    System.out.printf("%s's Account     Rate    Balance %n",name);
    System.out.printf("========================================== %n");
    System.out.printf("Saving           %s  %s%.2f %n",savRate,dollar,ibSav);
    System.out.printf("Check            %s  %s%.2f %n",checkRate,dollar,ibChq);
    System.out.printf("========================================== %n");

}

public void printFinalBalances() {

    String name = customerObj.getCustomerName();
    String savRate = "2.95%";
    String checkRate = "2.10%";
    String dollar = "$";

    System.out.printf("========================================== %n");
    System.out.printf("Final account balances after %d years %n",numYears);
    System.out.printf("========================================== %n");
    System.out.printf("%s's Account     Rate    Balance %n",name); //if name is longer than 
the example output ("John"), the headers; Rate and Balance shift further to the right 
outside of the 'table'
    System.out.printf("========================================== %n");
    System.out.printf("Saving           %s  %s%.2f %n",savRate,dollar,INTEREST_SAVING);
    System.out.printf("Check            %s  %s%.2f %n",checkRate,dollar,INTEREST_CHEQUE);
    System.out.printf("========================================== %n");
}



}

Any help with this would be greatly appreciated! Thanks.

Noods
  • 445
  • 4
  • 13

2 Answers2

1
private Customer customerObj;
private int numYears;
private final double INTEREST_SAVING = 
   customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);

You are asking to compute INTEREST_SAVING before customerObj is set to anything. So you get a NullPointerException. numYears will also still be 0 at that point.

This should probably not be a field, but a method with input parameters.

Thilo
  • 257,207
  • 101
  • 511
  • 656
1

The problem lies here

private final double INTEREST_SAVING = customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);
private final double INTEREST_CHEQUE = customerObj.getCheckAccount().getFinalBalance(0.0210,numYears);

Your initialization of bank based on

 Bank bankObj = new Bank();

As such your bankobj had the following values by default

private Customer customerObj; // Null

private int numYears; //0

As such when it tried to initialize the bankObj

it was basically doing

private final double INTEREST_SAVING =

customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);

null.getSavingAccount().getFinalBalance(0.0295,numYears);

Thus the null pointer exception

The thing is before creating the bank object you need to first have the customer and the accounts. Hence you cannot do

Bank bank = new Bank()

before

createCustomerAccounts();

I managed to solve it in the end with:

Bank.java

public class Bank {


private Customer customerObj;
private int numYears;
private double interest_saving = 0;
private double interest_cheque = 0;

    public Bank(Customer customer, int years){
        customerObj = customer;
        numYears = years;
        interest_saving = customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);
        interest_cheque = customerObj.getCheckAccount().getFinalBalance(0.0210,numYears);
    }

    public void printInitialBalances() {

        String name = customerObj.getCustomerName();
        double ibSav = customerObj.getSavingAccount().getInitialBalance();
        double ibChq = customerObj.getCheckAccount().getInitialBalance();
        String savRate = "2.95%";
        String checkRate = "2.10%";
        String dollar = "$";

        System.out.printf("========================================== %n");
        System.out.printf("Initial account balances %n");
        System.out.printf("========================================== %n");
        System.out.printf("%s's Account     Rate    Balance %n",name);
        System.out.printf("========================================== %n");
        System.out.printf("Saving           %s  %s%.2f %n",savRate,dollar,ibSav);
        System.out.printf("Check            %s  %s%.2f %n",checkRate,dollar,ibChq);
        System.out.printf("========================================== %n");

    }

    public void printFinalBalances() {

        String name = customerObj.getCustomerName();
        String savRate = "2.95%";
        String checkRate = "2.10%";
        String dollar = "$";

        System.out.printf("========================================== %n");
        System.out.printf("Final account balances after %d years %n",numYears);
        System.out.printf("========================================== %n");
        System.out.printf("%s's Account     Rate    Balance %n",name); //if name is longer than the example output ("John"), the headers; Rate and Balance shift further to the right outside of the 'table'
        System.out.printf("========================================== %n");
        System.out.printf("Saving           %s  %s%.2f %n",savRate,dollar,interest_saving);
        System.out.printf("Check            %s  %s%.2f %n",checkRate,dollar,interest_cheque);
        System.out.printf("========================================== %n");
    }    
}

Test.java

import java.util.Scanner;

public class Test{
    public static void main(String[] args) {



        Bank bankObj = createCustomerAccounts();

        bankObj.printInitialBalances();

        bankObj.printFinalBalances();

    }

    public static Bank createCustomerAccounts() {
        int numYears = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the customer's full name: ");
        String custName = keyboard.nextLine();
        System.out.println("Enter the number of years: ");
        numYears = keyboard.nextInt();
        System.out.println("Enter initial balance for the savings account: ");
        double ibSavings = keyboard.nextDouble();
        System.out.println("Enter initial balance for the cheque account: ");
        double ibCheck = keyboard.nextDouble();
        keyboard.close();

        Account savingAcc = new Account(ibSavings);
        Account checkAcc = new Account(ibCheck);
        Customer customerObj = new Customer(custName, savingAcc, checkAcc);
        Bank bankObj = new Bank(customerObj,numYears);

        return bankObj;
    }


}

I basically moved your code around

Jackson
  • 1,213
  • 1
  • 4
  • 14
  • Ok thanks. How would I fix it so that it doesn't recognise customerObj as null? – Noods May 10 '20 at 07:18
  • @Noods See my code edit. Messed up your code a little but it solved the main code logic problem. It runs on my end with correct data so should work for you. – Jackson May 10 '20 at 07:35
  • @Noods Not very beautiful but it should show you how to go about solving it and where the problem was – Jackson May 10 '20 at 07:46
  • INTEREST_SAVING and INTEREST_CHEQUE need to be declared as final – Noods May 10 '20 at 08:43