views:

65

answers:

5

Hi, I get the following error:

undefined method `[]' for nil:NilClass

The related snippet in line 50:

 47: <%=h @contact.date_entered.to_date %></br>  
 48: Next event: 
 49: <% next_delayed_todo = @contact.next_delayed_todo %> 
 50: <% unless next_delayed_todo[:event].nil? %>
 52: <%= next_delayed_todo[:event].title %> </br>
+2  A: 

It means that you called [] on a nil object. In this case, next_delayed_todo was nil.

You want something more like: unless next_delayed_todo.nil? || next_delayed_todo[:event].nil?

kwerle
Hi, I do have the following code a few lines up: <% unless next_delayed_todo.nil? || next_delayed_todo[:event].nil? %> hmm...so not exactly sure what to do...
Angela
ah, interesting...it's in my local but not on heroku I guess...let me make some adjustments.
Angela
+2  A: 

It means that the NilClass doesn't implement the [] method. Which in turn means that, in your code, next_delayed_todo is nil.

the .nil? you have now checks if the value returned by next_delayed_todo[:event] is nil. You should also add a nil check for next_delayed_todo

Rutger
+1  A: 

See how you have 3 consecutive lines of nothing but code in your view? That's a sign you should pull it out into a helper, in order to keep your views clean.

New view code:

<%=h @contact.date_entered.to_date %></br>  
Next event: <%= next_delayed_todo(@contact) %> </br>

Then in your helper:

def next_delayed_todo(contact)
  contact.next_delayed_todo[:event].title rescue ""
end

Note that the error you were getting is because of next_delayed_todo being nil. The first line of the helper method uses rescue "" to set an alternate value if it is nil. You can replace it with rescue "none." or any other string that makes sense.

Mark Thomas
although didn't answer the question, this is helpful change I need to make...thanks +1
Angela
It did have a *solution* to your question, but I have added a note that makes it explicit.
Mark Thomas
ah, yes -- the rescue also is a key component to the solution I see....
Angela
A: 

NoMethodError: is an class derived from exception, is an exception. In a strong dynamically typed language if you try to use a method on types for which that method doesn't exist you throw a bad type exception.

method_missing is a function -http://ruby-doc.org/core/classes/Kernel.html#M005925

Invoked by Ruby when obj is sent a message it cannot handle. symbol is the symbol for the method called, and args are any arguments that were passed to it. By default, the interpreter raises an error when this method is called. However, it is possible to override the method to provide more dynamic behavior. The example below creates a class Roman, which responds to methods with names consisting of roman numerals, returning the corresponding integer values.

I think(don't trust me on this) the default method_missing function does something like a NoMethodError with the message "undefined method `#{method_name}' for #{inspectVariableValue}:#{ClassName}".

Roman A. Taycher
A: 

I see an error in your code, unless should start a block, otherwise, like it is written the line does nothing. So this should work:

 <%=h @contact.date_entered.to_date %></br>  
 Next event: 
 <% next_delayed_todo = @contact.next_delayed_todo %> 
 <% unless next_delayed_todo[:event].nil? do %>
   <%= next_delayed_todo[:event].title %> </br>
 <% end %>
nathanvda