I'm having trouble with a homework assignment, creating a class that calls methods from another class. We are given the following class:
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
/**
Deposits money into this account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
Makes a withdrawal from this account, or charges a penalty if
sufficient funds are not available.
@param amount the amount of the withdrawal
*/
public void withdraw(double amount)
{
final double PENALTY = 10;
if (amount > balance)
{
balance = balance - PENALTY;
}
else
{
balance = balance - amount;
}
}
/**
Adds interest to this account.
@param rate the interest rate in percent
*/
public void addInterest(double rate)
{
double amount = balance * rate / 100;
balance = balance + amount;
}
/**
Gets the current balance of this account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}
I'm then given the following prompt:
"Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount. Implement four methods:
- public void deposit(double amount, String account)
- public void withdraw(double amount, String account)
- public void transfer(double amount, String account)
- public double getBalance(String account)
Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected. For a transfer, it indicates the account from which the money is taken; the money is automatically transferred to the other account."
So here is what I've done:
public class Portfolio
{
BankAccount checking;
BankAccount savings;
public void deposit(double x, String y)
{
if (y.equals("C"))
{
checking.deposit(x);
}
else if (y.equals("S"))
{
savings.deposit(x);
}
}
public void withdraw(double x, String y)
{
if (y.equals("C"))
{
checking.withdraw(x);
}
else if (y.equals("S"))
{
savings.withdraw(x);
}
}
//public void transfer(double amount, String account)
//{
// add later
//}
public double getBalance(String account)
{
if (account.equals("C"))
{
return checking.getBalance();
}
else
{
return savings.getBalance();
}
}
}
But then I can't even get the deposit method to work. When I run this program...
public class PortfolioTester
{
public static void main(String [] args)
{
Portfolio money = new Portfolio();
money.deposit(700, "S");
}
}
I get this error:
Exception in thread "main" java.lang.NullPointerException at Portfolio.deposit(Portfolio.java:14) at PortfolioTester.main(PortfolioTester.java:6)
I guess I'm fundementally misunderstanding something about how classes work. Could someone point me in the right direction?
BankAccount checking;
is declaring a field of typeBankAccount
, but it's not initializing it, so it's still null. You need to assignnew BankAccount()
to it somewhere. Perhaps in the constructor forPortfolio
.