Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Foundations Methods Class Methods

Nicholas Lee
Nicholas Lee
12,474 Points

I keep getting, "NoMethodError: undefined method `find' for nil:NilClass"

''' class BankAccount def self.create_for(first_name, last_name) @account ||= [] # conditional assignment operator - assigns valye to a variable if it does not already exist @account << BankAccount.new(first_name, last_name) end

def self.find_for(first_name, last_name) @accounts.find{|account| account.full_name == "#{first_name} #{last_name}"} end

def initialize(first_name, last_name) @balance = 0 @first_name = first_name @last_name = last_name end

def full_name "#{@first_name} #{@last_name}" end

def deposit(amount) @balance += amount end

def withdraw(amount) @balance -= amount end end '''

When I type BankAccount.find_for("Nicholas","Lee") I keep receiving an error message 2.1.1 :007 > BankAccount.find_for("Nicholas","Lee") NoMethodError: undefined method find' for nil:NilClass from /Users/nicholaslee/Desktop/newbank.rb:8:infind_for' from (irb#2):7 2.1.1 :008 >

Can anyone see what is wrong with my code? I have double and triple checked it.

I'v spent two days on this video and it could not be going any slower. It is also lovely how small his coding is on the video. I can hardly tell the difference between ( ) and { }

2 Answers

Carles Jove i Buxeda
Carles Jove i Buxeda
4,281 Points

Hi Nicholas. The error message is:

NoMethodError: undefined method find' for nil:NilClass

That points you where to look to solve the error. Since it is saying undefined method find, let's have a look at where you're using this method. This is where:

@accounts.find{|account| account.full_name == "#{first_name} #{last_name}"}

The error message follows for nil:NilClass. This means that you're applying the find method to either an unexisting class or object. So let's have a closer look at this:

@accounts.find

A find is being performed on @accounts, which is an instance variable, so let's look at the rest of the code for @accounts. Do you notice anything?

Yes! Previously, in the create_for method, the variable was set as @account (singular), not @accounts (plural). Hence the error ;-) Rewrite the buggy line of code as this, and it'll work:

@account.find

Cheers.

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

OK, here's the thing:

Your @account instance variable is defined in singular and later used in plural, so it can't work. Change your code snippet to this:

  def self.create_for(first_name, last_name)
    @accounts ||= []
    @accounts << BankAccount.new(first_name, last_name)
  end

Now run irb, load the file using load './file_name.rb' (replace the file_name with the name of your real file) AND create an account first - this will initialize the @accounts variable and from now on you can use find_for properly.

Nicholas Lee
Nicholas Lee
12,474 Points

Thank you very much. These small bugs really get me sometimes!