Undefined method `[]' for nil:NilClass

Hi,

I’m currently workin on dashing graph to fetch data from Nagios through
json by implementing a solution similar to this
http://thomasvm.github.io/blog/2012/11/19/bling-dashboards-using-graphite-and-dashing/

The problem is, it keeps on throwing the following exception and have no
idea where I went wrong.

scheduler caught exception:
undefined method []' for nil:NilClass /root/dashtest/lib/graphite.rb:13:inget_value’
/root/dashtest/lib/graphite.rb:54:in value' /root/dashtest/jobs/graphite.rb:12:inblock in <top (required)>’
/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in
call' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:intrigger_block’
/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in
block in trigger' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:incall’
/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in
`block in trigger_job’

Sorry if this has been asked before but I’ve hit nothing but dead ends
so far and I’ve been googling it for several
days with no luck.

def get_value
data = {}
return data[:some_key]
end

p get_value #=>nil
result = get_value[0] + 1

–output:–
1.rb:6:in <main>': undefined method[]’ for nil:NilClass
(NoMethodError)

have no
idea where I went wrong.

After every line in your code, output the value of every variable in
existence. You will see that one of the values is nil.

Thanks dude. I finally found where it went wrong. However, I’m getting a
new exception after fixing it.

================================================================================
scheduler caught exception:
wrong number of arguments (1 for 2)
/root/dashtest/lib/graphite.rb:31:in points' /root/dashtest/jobs/graphite.rb:13:inblock in <top (required)>’
/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in
call' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:intrigger_block’
/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in
block in trigger' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:incall’
/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in
`block in trigger_job’

with the lines in question:

#jobs/graphite.rb:13
points = q.points “S10_PUE” “-3d”

#lib/graphite.rb:31
def points(name, since)
stats = query name, since
datapoints = stats[:datapoints]

    points = []
    count = 1

    (datapoints.select { |el| not el[0].nil? }).each do|item|
        points << { x: count, y: get_value(item)}
        count += 1
    end

    return points

Multiple arguments are seperated by commas:

q.points “S10_PUE” “-3d”

q.points “S10_PUE”, “-3d”

Yeah, I had it fixed right after I posted but still getting this

================================================================================
scheduler caught exception:
wrong number of arguments (0 for 2)
/root/dashtest/lib/graphite.rb:31:in points' /root/dashtest/lib/graphite.rb:43:inpoints’
/root/dashtest/jobs/graphite.rb:13:in block in <top (required)>' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:incall’
/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in
trigger_block' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:inblock in trigger’
/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in
call' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:inblock in trigger_job’

I’m guessing it couldn’t have been that one. :frowning:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs