Skip to content
Poignant Guide Ruby Q&A

What does undefined method for nil:NilClass mean, and how do I fix it?

Staff answerWritten by Fenwick Hale · Errors and debugging help0 replies

Short answer

This error means you called a method on nil, Ruby's value for nothing, and nil does not have that method. The bug is almost never the method name. It is the earlier line where a value you expected did not arrive. Find which variable is nil, find why, then fix the source or guard the call.

Written by Fenwick Hale · Staff: gems, Rails, tooling and debugging ·
CitePoignant Guide Ruby Q&A, "What does undefined method for nil:NilClass mean, and how do I fix it?", https://poignantguide.net/answers/read/what-does-undefined-method-for-nilnilclass-mean-and-how-do-i-fix-it/, accessed 2026-11-17.
  1. Staff: gems, Rails, tooling and debugging ·

    What the message says

    A NoMethodError is Ruby's report that you sent a message (called a method) to an object that has no such method. The NoMethodError documentation describes it as raised when a method is called on a receiver which does not have it defined. The receiver is the object on the left of the dot. Here is the classic case:

    user = { name: "Ada" }
    puts user[:email].upcase
    # => undefined method `upcase' for nil (NoMethodError)
    

    user[:email] finds no key called :email, so it returns nil. Then nil.upcase fails. The wording depends on your Ruby. Older versions, and some pages of the Ruby docs, print for nil:NilClass. The Ruby 3.3 release notes say NoMethodError messages stopped using the object's inspect text and now name the class instead, so you will see the shorter for nil in Ruby 3.3. Both mean the same thing: the receiver was nil.

    The NilClass documentation calls it the class of the singleton object nil, which means there is only one nil and it has only a short list of methods, such as nil?, to_s and to_a. That is why upcase, length, each and + on nil all fail.

    Where nil comes from

    1. A missing hash key or array position. {}[:x] and [1, 2][5] both return nil.
    2. A method that returns nil. puts returns nil, so name = puts("hi") leaves name empty.
    3. A variable that was never set. An instance variable like @name is nil until you assign it. This is common inside a class where initialize never set it.
    4. A search that found nothing. The Ruby docs show "Python is fascinating!".match(/(ruby) is (\w+)/i), which returns nil because there is no match, and then .values_at fails.
    5. A method whose last line is not the value you meant. Ruby returns the last expression, and if that line is a puts or an if that did not run, the method hands back nil.

    Find the nil, then choose a fix

    Print the suspect with p, since p shows nil as the word nil, where puts shows a blank line:

    email = user[:email]
    p email    # => nil
    

    Now pick the fix that matches your intent:

    Fix Example Use when
    Fix the source Add email: when you build the hash The value should always exist
    Default with || (user[:email] || "").upcase A missing value has a sensible stand-in
    Hash#fetch user.fetch(:email, "none") You want a default or a loud error for a missing key
    Safe navigation &. user[:email]&.upcase Missing is fine, and nil is an acceptable result
    Explicit check puts "No email" if email.nil? You need different behavior for missing

    The calling methods documentation explains the safe navigation operator &.: it skips the call when the receiver is nil and returns nil, without evaluating the arguments. It also warns that &. skips only the next call, so a longer chain needs it at each step:

    m = "Python is fascinating!".match(/(ruby) is (\w+)/i)
    p m&.values_at(1, 2)             # => nil
    p m&.values_at(1, 2)&.join(" ")  # => nil
    

    A word of caution: &. hides the problem as well as the error. If a value should never be missing, a loud failure is more useful, so prefer fetch without a default, which raises a KeyError that names the missing key.

    user.fetch(:email)
    # => key not found: :email (KeyError)
    

    Mistakes beginners make

    1. Fixing the line in the message instead of the line before it. The error line is where nil was used, not where it appeared.
    2. Adding &. everywhere. The program stops crashing and starts giving blank output instead, which is harder to debug.
    3. Misspelling a hash key, or mixing symbols and strings. user["name"] returns nil when the key is :name. See the symbols answer linked below.
    4. Ending a method with puts. The method then returns nil to its caller.

    Check this in order

    1. Read the first line of the error: the method name, and nil as the receiver.
    2. Read the next line of the backtrace for the file and line number where it happened.
    3. On that line, find the object to the left of the dot. That is your nil.
    4. Print it: p thing. Confirm it is nil rather than a string such as "nil".
    5. Walk backward: where was it assigned? Did a hash lookup, a search, or a method call give it?
    6. Decide whether missing is allowed. If not, fix the source or use fetch. If it is, use a default or &..
    7. Run again, and test the missing case on purpose.

    For help printing values, see What is the difference between puts, print and p in Ruby?. For the key mismatch in item 3, see What is a symbol in Ruby, and when should I use one instead of a string?. If the nil came out of a block, What is a block in Ruby, and what does yield do? explains what a block hands back. The guide's Chapter 5 is a good companion once you start building classes, where nil instance variables are common.

    Checked on 12 November 2026.

    1
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

Poignant Guide Ruby Q&A. Questions and answers on learning Ruby and programming: syntax, blocks, objects, gems, Rails, debugging and reading the Poignant Guide.