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.