1

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?

2
  • 1
    BankAccount checking; is declaring a field of type BankAccount, but it's not initializing it, so it's still null. You need to assign new BankAccount() to it somewhere. Perhaps in the constructor for Portfolio.
    – Blorgbeard
    Nov 11, 2014 at 0:13
  • ooohhh, I think I understand now, thanks. I might fire another question if I can't figure it out now, but thank you!
    – thissucks
    Nov 11, 2014 at 0:33

2 Answers 2

1

You are getting those errors because you have to initialize the BankAccount classes that are used in the deposit method. I would recommend putting a constructor in the Portfolio class (Classes should usually have constructors that initialize the class attributes). Inside the Portfolio constructor, you could initialize the BankAccount classes "checking" and "savings".

Remember for non-static methods, you have to have an instance of a class to call its methods.

0
1

The problem is that you've created a BankAccount in the Portfolio class, but haven't initialized them. It's like doing this:

int myInt;
System.out.println(myInt);

What happens? You won't be able to compile that, because it hasn't been initialized. But when your variables are inside classes, inside other classes, the compiler can't tell as easily whether it's been initialized or not. You have to create a constructor for your Portfolio class and initialize the BankAccount objects, like so:

public class Portfolio
{
    BankAccount savings;
    BankAccount checking;

    public Portfolio()
    {
        savings = new x();  //Replace x with what you think needs to be there
        checking = new x(); //Replace x with what you think needs to be there
    }
}
1
  • Thanks, this is a very clear and helpful response as well, I'd feel silly if I didn't feel so relived.
    – thissucks
    Nov 11, 2014 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.