Ruby: undefined method `[]' for nil:NilClass when trying to get Enumerator on an Array of Hashes
undefined method `[]' for nil:NilClass
says you tried to do something[index]
but something
is nil
. Ruby won't let you use nil
as an array (ie. call the []
method on it).
The problem is not on the attributs.each
line but on the line following which calls the []
method on attribut
.
typeAttribut = attribut['objectTypeAttribute']
This indicates something in attributs
is nil. This could happen if attributsParam
is a list that contains nil like so.attributsParam = [nil];
attributs = Array(attributsParam);
# [nil]
puts attributs.inspect
Simplest way to debug it is to add puts attributs.inspect
just before the loop.Also consider if you really need the attributs = Array(attributsParam)
line or if it's already something Enumerable.
Ruby: undefined method `' for nil:NilClass error message
@students
refers to an empty array.
Does it? (Apparently not, because the error message says it's nil
!!) You never showed us where this variable get initialised.In ruby, all instance variables evaluate to nil
by default, if you don't define them as something else.
Undefined method for NilClass
In your first loop iteration new_matrix[h_entries]
would be nil
. So, you can't use <<
method on it.
Try to initialize it to an empty array if it is nil
:
new_matrix[h_entries] ||= []
new_matrix[h_entries] << ([array[h_entries][v_entries]])
Undefined method `%' for nil:NilClass (NoMethodError)
The elements of range
are 1 to 100, but the indices into range
are 0 to 99. The each
method already gives you the elements of the array, not the indices — just use i
inside the loop body instead of range[i]
.
You would have gotten a more reasonable error if you indexed an array a
with a.fetch(i)
instead of a[i]
— the former throws IndexError
while the latter gives nil
.
How to catch an undefined method `[]' for nil:NilClass error?
A bit late to the party, but, as pointed in this answer, Ruby 2.3.0 introduced a new method called dig
, which would return nil
if one of the chained keys is nil
. Your omniauth auth hash could then be presented as:
omniauth = {
...
"extra"=>{ "raw_info"=>
{ "location"=>"New York",
"gravatar_id"=>"123456789"}}
...
}
omniauth.dig('extra',
'raw_info',
'location',
'name',
'foo',
'bar',
'baz') #<= nil
Undefined method `+' for nil:NilClass (NoMethodError)
Local variable reference has precedence over method call with the same name.
Similarly, local variable assignment has precedence over method call with the same name. A writer method of the form foo=
needs an explicit receiver. When the receiver is omitted, it is not recognized as a method, but as a local variable assignment.
def add_cash(amount)
self.cash = cash + amount
end
Why do I get undefined method for nil:NilClass on an object that clearly exists?
I'm pretty sure that the record you inspect (Daily with id=1030) and the failing [non-existing] record are two different records. Here's how you can find out. Amend your method like this, temporarily:
def daily_data_for(store, month, day, year, field)
daily = Daily.where(store_id: store, month: month, day: day, year: year).first
raise "Daily not found for #{store.inspect}, #{month.inspect}, #{day.inspect}, #{year.inspect}" unless daily
daily.send(field)
end
Now the error message should be much more revealing. undefined method `errors' for nil:NilClass in rails
You are using nested routes but in form_with
you have not specified that. Try to change the form_with
like this:
form_with(model: stage, url: [@project, stage])
Source: https://stackoverflow.com/a/46919792/4207394You will have to check the variable names, i.e., the stage
variable should have the new Stage
object and @project
variable should have the Project
object or else please change the names accordingly.
undefined method `-' for nil:NilClass in ruby model
You can't call or chain methods on a nil object. You're trying to subtract something from nil
which the error has told you. So you can fix this with:
def seat_avaliable
if seat.is_a?(Integer) && students&.any?
seat - students.count > 0
end
end
Change your view code to this<% if bus.seat_available %><td>Seat Available</td><% end %>
Related Topics
After_Save Callback to Set The Updated_By Column to The Current_User
When Is The Enumerator::Yielder#Yield Method Useful
Require Tree in Asset Pipeline
Ruby Require 'File' Doesn't Work But Require './File' Does. Why
Fastest Way to Skip Lines While Parsing Files in Ruby
Using Google Search Rest API in Ruby
How to Add a User Interrupt to an Infinite Loop
Why Is Split(' ') Trying to Be (Too) Smart
Persistent Tcp Connection in Rails App
Functional Testing of a Restful Post in Ruby on Rails
How to Interact with a Caldav Server from Ruby
Loading/Unloading/Updating Class in Ruby
Browsing Ruby Code a La Smalltalk
Sinatra Not Persisting Session with Redirect on Chrome