[英]ruby NoMethodError undefined method `balance' for nil:NilClass
Here is my code and this is the error I am receiving: NoMethodError undefined method 'balance' for nil:NilClass
这是我的代码,这是我收到的错误: 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: 错误在transfer
方法内,在此行中:
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. @account
尚未在任何地方设置,因此它为nil
并且在其上调用balance
时会出现NoMethodError
。
Maybe you meant account
, since you are passing it as an argument. 也许您的意思是account
,因为您将其作为参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.