Forum: Ruby NoMethodError: undefined method `traverse' for nil:NilClass

A9cf56f3df21e0355372d9c2eae8fc81?d=identicon&s=25 Mr. Bill (weloki)
on 2011-03-28 21:59
I'm using nokogiri to extract content from web pages and every now and
then I see this error thrown for certain pages, along with other
versions of the NoMethodError. I'm trying to understand the nature of
the error so I can be in a better position to provide fixes. The
relevant code and error message are pasted below.

nokogiri_helper.rb:

require 'nokogiri'
module NokogiriHelper

  def content_elements_for(node)
    nodes = []
    # We add node.parent because node is an
    # instance of Nokogiri::XML::Text
    # but we want the tag that contains the text.
    node.traverse{|element| nodes << element.parent if element.text? &&
element.parent.name != 'a' && has_content?(element.text)}
   ...
  end
 ...
end


ERROR (ContentExtractor)
#<NoMethodError: undefined method `traverse' for nil:NilClass>
/opt/plugins/helpers/nokogiri_helper.rb:68:in `content_elements_for'
/opt/plugins/ContentExtractor.rb:66:in `translate_one'
/opt/plugins/Translator.rb:24:in `translate'
/opt/plugins/Translator.rb:21:in `each'
/opt/plugins/Translator.rb:21:in `translate'
/opt/plugins/Translator.rb:16:in `execute'
/opt/plugins/ContentExtractor.rb:53:in `execute'
/opt/plugins/BasePlugin.rb:201:in `call'
/opt/lib/filesystem_lock_provider.rb:83:in `lock'
/opt/plugins/BasePlugin.rb:200:in `call'
/opt/lib/feed.rb:251:in `run'
/opt/lib/feed.rb:248:in `each'
/opt/lib/feed.rb:248:in `run'
/opt/lib/feed.rb:243:in `each'
/opt/lib/feed.rb:243:in `run'
/opt/lib/schedule_runner.rb:159:in `create_feed_process'
/opt/lib/schedule_runner.rb:154:in `fork'
/opt/lib/schedule_runner.rb:154:in `create_feed_process'
/opt/lib/schedule_runner.rb:325:in `run_iteration'
/opt/lib/schedule_runner.rb:269:in `run'
/opt/lib/schedule_runner.rb:268:in `loop'
/opt/lib/schedule_runner.rb:268:in `run'
bin/run_feed:57
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2011-03-29 01:39
1)
data = nil

data.each do |element|
  puts element
end

--output:--
Line 3: undefined method `each' for nil:NilClass (NoMethodError)


2)
def my_method(arr)
  arr.each do |element|
    puts element
  end
end


data = nil
my_method(data)

--output:--
Line 2:in `my_method': undefined method `each' for nil:NilClass
(NoMethodError)
  from t.rb:9


3)
def my_method(arr)
  arr.each do |element|
    puts element
  end
end

def get_data
   if 10 > 20
      return [10, 20, 30]
   end
end


data = get_data
my_method(data)

--output:--
Line 2:in `my_method': undefined method `each' for nil:NilClass
(NoMethodError)
  from t.rb:15


That is the nature of your error.
3d254a392475658893547eacd44bfd67?d=identicon&s=25 Gerard Cahill (Guest)
on 2011-03-30 11:39
(Received via mailing list)
You're invoking a method on a nil object causing a runtime error
(i.e the nil class does not define a method 'traverse').
Try checking the object first before calling the method.
Something simple like: unless node.nil? should do it. In 7stud's
example,
the variable 'data' is nil so calling a method on it, in this case
'each',
raises the same error.
3d254a392475658893547eacd44bfd67?d=identicon&s=25 Gerard Cahill (Guest)
on 2011-03-30 11:39
(Received via mailing list)
You're invoking a method on a nil object causing a runtime error (i.e
the nil class does not define a method 'traverse'). Try checking the
object first before calling the method. Something simple like: unless
node.nil? should do it. In 7stud's example, the variable 'data' is nil
so calling a method on it, in this case 'each', raises the same error.
A9cf56f3df21e0355372d9c2eae8fc81?d=identicon&s=25 Mr. Bill (weloki)
on 2011-03-30 16:55
Thank you both very much. This helps immensely.
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.