Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,441 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!