Here is my code and this is the error I am receiving:
NoMethodError
undefined method 'balance' for nil:NilClass
I am new to programming and can't seem to figure out the problem.
class CheckingAccount < BankAccount
attr_reader :number_of_withdrawals
MAX_FREE_WITHDRAWALS = 3
def initialize(balance)
balance = balance
super(balance)
@number_of_withdrawals = 0
end
def get_free_withdrawal_limit
MAX_FREE_WITHDRAWALS
end
def transfer(account, amount)
@other_account = CheckingAccount.new(amount)
current_balance = @account.balance
other_balance = @other_account.balance
if current_balance > amount
current_balance = @balance - amount
other_balance = @balance + amount
elsif
current_balance < amount
"not enought funds available for transfer"
end
The error is within
transfer
method, in this line:@account
has not been set anywhere, so it isnil
and you get aNoMethodError
when you callbalance
on it.Maybe you meant
account
, since you are passing it as an argument.