ruby NoMethodError undefined method `balance' for nil:NilClass

404 views Asked by At

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
1

There are 1 answers

0
Gerry On

The error is within transfer method, in this line:

current_balance = @account.balance

@account has not been set anywhere, so it is nil and you get a NoMethodError when you call balance on it.

Maybe you meant account, since you are passing it as an argument.