Undefined Method '>' for Nil:Nilclass <Nomethoderror>

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 On Rails - NoMethodError: undefined method `[]' for nil:NilClass

From the stacktrace you posted, this line in particular appears to be the issue:

Rails.application.credentials[Rails.env.to_sym][:stripe][:public_key]

Since you're chaining multiple hash accesses, one of those objects is nil. You could try inspecting each of them by adding something like this to the top of the initializer:

puts Rails.application.credentials.inspect
puts Rails.application.credentials[Rails.env.to_sym].inspect
puts Rails.application.credentials[Rails.env.to_sym][:stripe].inspect

That will tell you which part is nil that shouldn't be, and you can update your credentials file accordingly.

undefined method `' for nil:NilClass Ruby on Rails (NoMethodError)

It appears one or more of your appointment records has a nil appointment_time.

If this is not expected, you should add a validation on the model that it's not null, and fix your existing data.

Otherwise (and this works as quick fix), you can chain a query to not include records with a nil appointment time:

  def upcoming_appointments
appointments
.order(appointment_time: :desc)
.where.not(appointment_time: nil)
.select { |a| a.appointment_time > (DateTime.now) }
end

If where.not isn't available (if you're using an older version of rails), you can also use where("appointment_time != null")

Ruby on Rails - undefined method `[]' for nil:NilClass when using acts_as_votable gem

upvote_by seems to be an alias of vote_up, maybe you are not using the same gem version from the tutorial?

https://github.com/ryanto/acts_as_votable/blob/599995f7ec5aa0f8a04312768fc956e9003d32d4/lib/acts_as_votable/votable.rb#L15

Try using vote_up instead, since it looks like the original method, it should work on versions where the upvote_by alias was not set.

Undefined method '' for nil:NilClass NoMethodError

how come > is considered as a method but it is a logical operator?

There is no problem with that. In Ruby, when you write an expression like 1 + 2, internally it is understood as 1.+( 2 ): Calling method #+ on the receiver 1 with 2 as a single argument. Another way to understand the same is, that you are sending the message [ :+, 2 ] to the object 1.

what is the cause of the error?

Now in your case, @state_turns[ state.id ] returns nil for some reason. So the expression @state_turns[state.id] > 0 becomes nil > 0, which, as I said earlier, is understood as calling #> method on nil. But you can check that NilClass, to which nil belongs, has no instance method #> defined on it:

NilClass.instance_methods.include? :> # => false
nil.respond_to? :> # => false

The NoMethodError exception is therefore a legitimate error. By raising this error, Ruby protects you: It tells you early that your @state_turns[ state.id ] is not what you assume it to be. That way, you can correct your errors earlier, and be a more efficient programmer. Also, Ruby exceptions can be rescued with begin ... rescue ... end statement. Ruby exceptions are generally very friendly and useful objects, and you should learn how to define your custom exceptions in your software projects.

To extend this discussion a bit more, let's look at from where your error is coming. When you write an expression like nil > 10, which is actually nil.>( 10 ), Ruby starts searching for #> method in the lookup chain of nil. You can see the lookup chain by typing:

    nil.singleton_class.ancestors #=> [NilClass, Object, Kernel, BasicObject]

The method will be searched in each module of the ancestor chain: First, Ruby will check whether #> is defined on NilClass, then on Object, then Kernel, and finally, BasicObject. If #> is not found in any of them, Ruby will continue by trying method_missing methods, again in order on all the modules of the lookup chain. If even method_missing does not handle the :> message, NoMethodError exception will be raised. To demonstrate, let's define #method_missing method in Object by inserting a custom message, that will appear instead of NoMethodError:

class Object
def method_missing( name, *args )
puts "There is no method '##{name}' defined on #{self.class}, you dummy!"
end
end

[ 1, 2, 3 ][ 3 ] > 2
#=> There is no method '#>' defined on NilClass, you dummy!

Why doesn't it says like NullPointerException

There is no such exception in Ruby. Check the Ruby's Exception class.

Ruby on rails: undefined method `[]' for nil:NilClass?

I kind of regenerated your issue

2.1.1 :003 > a=nil
=> nil
2.1.1 :004 > a['asd']
NoMethodError: undefined method `[]' for nil:NilClass
from (irb):4
from /home/illu/.rvm/rubies/ruby-2.1.1/bin/irb:11:in `<main>'
2.1.1 :005 >

In your case it probably
params[:stock_availabilities] is giving nil and you are trying to access the key :stock_id in the nil class.
I suggest you to pry the values at the point.

EDIT1:

After having a look at your server log it is clear that the key stock_availabilities you are trying to access is actually stockavailability

your code should be like

# though no :stock_id key/value is found in your server log
@stock = Stock.find(params[:stockavailability][:stock_id])

in `_main': undefined method `run' for nil:NilClass (NoMethodError)

At the point where you call request.run the value for request is nil. This is why you are seeing the error you're given.

This is happening because the line right above it assigns the nil value to the request variable.

You are clearly coming from another language that is not Ruby (some type of C maybe?), by how you've formatted things. It would help for you to get more familiar with Ruby and its idioms. However, best I can tell, you want to do something like this instead:

def _main
request = MySqliteRequest.new
request.from('nba_player_data.csv')
request.select('name')
request.where('birth_state', 'Indiana')
request.run
end
_main()

This assumes you've also defined (and in some cases probably overridden) these methods on your MySqliteRequest Object or Model:

from

select

where

However, please note that the way you're going about this is just completely against how Ruby and Ruby on Rails is designed to work.



Related Topics



Leave a reply



Submit