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

David Jarrin
PLUS
David Jarrin
Courses Plus Student 11,182 Points

NoMethodError: undefined method '<<' for nil:NilClass

I am trying to create a class method that creates an object in an array. My method looks like this

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

I keep getting this error when I try to load the file in the console

NoMethodError: undefined method `<<' for nil:NilClass         
        from bank_account.rb:5:in `create_for'                
        from bank_account.rb:27:in `<top (required)>'         
        from (irb):5:in `load'                                
        from (irb):5                                          
        from /usr/local/bin/irb:11:in `<main>' 

I have a feeling this is probably a common error but I am a little stuck. Let me know if you need anymore of the code to help me diagnose the problem....Thanks.

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi David,

You appear to be missing an "s" on the below line, simply add that in and it should work as expected.

@account ||= []
David Jarrin
David Jarrin
Courses Plus Student 11,182 Points

two of my questions answered! (well still working on the other one) Thanks for spotting my mistake

Chris Shaw
Chris Shaw
26,676 Points

No problem, I tend to find go backwards by removing lines helps to see where an error first occurs as in my case I can usually spot the error straight away whereas looking at the code for 15 minutes just hurts too much.