1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

undefined method `to_sym' for nil:NilClass


Problem: Seemlingly at random, you've encountered the error undefined method `to_sym' for nil:NilClass, or a similar error message for any method name other than to_sym.

Solution: This problem can be a tough one to solve, and there's more than one cause. The reason why you're seeing this message is always the same: the object you're trying to work with is the nil object. The nil object represents nothing. Why is this object nil? More often than not, it's one of two reasons.

  1. A variable wasn't initialized. If you use a variable without it being initialized, it will return nil. This is especially true with instance variables, where they don't have to be assigned to before they can be referred to. Read back through your code and make sure all your variables are initialized.
  2. The value nil was returned from a method. Some methods, when they fail to perform their task for some reason, don't raise an exception. It's just not appropriate for all methods to do this. Instead, they'll often return nil. If this is the case, find which method is returning nil and try to determine why it's doing this.
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. FAQs
  6. Error - Undefined Method "to_sym" for nil:NilClass

©2014 About.com. All rights reserved.