0

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

Line 76 is:

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

Line 84 is:

Bank bankObj = new Bank();

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 aware that this is a null pointer issue, but I am not sure how to fix this for my 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");
}



}
Noods
  • 445
  • 4
  • 13
  • Please remember the *minimal* part of your [mcve]. By simplifying your code it will be easier to see problems, and it will be easier to debug. And you might even figure out the problem yourself when creating the [mcve]. – Some programmer dude May 10 '20 at 08:57
  • Line `76` throws: `at Bank.(Bank.java:76)`. In that line you have something like `variable.method()` where `variable` is `null`. So you try to access something that does not even exist. Backtrack where you thought you would assign it, `variable = ...` conclude that this either returned `null` or was never executed before line `76`. Repeat until you found your logic bug. Use a debugger or multiple print statements to investigate your variables. – Zabuzard May 10 '20 at 09:00
  • Your two fields `private final double INTEREST_SAVING = customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);` (and the other) are executed before `customerObj` is assigned any value. So there is no `customerObj = ...` happening before executing above. Hence `customerObj` is still `null` and it crashes on construction of your banks. I.e. it makes no sense to attempt to get the saving account of a customer who does not even exist. – Zabuzard May 10 '20 at 09:02
  • @Zabuzard I edited the lines again. I accidentally gave you the wrong line numbers. I understand that customerObj has not been assigned a value yet. However, I'm not sure how else I can assign the correct value to INTEREST_SAVING and INTEREST_CHEQUE fields. – Noods May 10 '20 at 09:10
  • You first have to have a customer. It makes absolutely no sense to attempt to get the saving account of a customer who does not exist yet. Which customer? You have to define that first, the logic just makes no sense currently. It is like you walk into the super market "I like to buy an item" "which item?" "i dont know yet, but i want to buy it". You can not buy something you havent decided yet, first you need an item, then you can buy it. – Zabuzard May 10 '20 at 09:26

0 Answers0