Forum: Ruby undefined method `[]' for nil:NilClass

23ee87ae970504ef5c1ba0d1514fe93d?d=identicon&s=25 Jem F. (jem_f)
on 2014-06-26 10:48
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-da...

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:in `get_value'
/root/dashtest/lib/graphite.rb:54:in `value'
/root/dashtest/jobs/graphite.rb:12:in `block 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:in
`trigger_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:in
`call'
/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.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2014-06-26 17:38
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.
23ee87ae970504ef5c1ba0d1514fe93d?d=identicon&s=25 Jem F. (jem_f)
on 2014-06-27 16:09
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:in `block 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:in
`trigger_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:in
`call'
/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
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2014-06-29 19:53
Multiple arguments are seperated by commas:

q.points "S10_PUE" "-3d"

q.points "S10_PUE", "-3d"
23ee87ae970504ef5c1ba0d1514fe93d?d=identicon&s=25 Jem F. (jem_f)
on 2014-06-30 13:39
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:in `points'
/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:in
`call'
/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:in
`block 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:in
`block in trigger_job'
================================================================================


I'm guessing it couldn't have been that one. :(
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.