NoMethodError: undefined method `traverse' for nil:NilClass

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:incontent_elements_for’
/opt/plugins/ContentExtractor.rb:66:in translate_one' /opt/plugins/Translator.rb:24:intranslate’
/opt/plugins/Translator.rb:21:in each' /opt/plugins/Translator.rb:21:intranslate’
/opt/plugins/Translator.rb:16:in execute' /opt/plugins/ContentExtractor.rb:53:inexecute’
/opt/plugins/BasePlugin.rb:201:in call' /opt/lib/filesystem_lock_provider.rb:83:inlock’
/opt/plugins/BasePlugin.rb:200:in call' /opt/lib/feed.rb:251:inrun’
/opt/lib/feed.rb:248:in each' /opt/lib/feed.rb:248:inrun’
/opt/lib/feed.rb:243:in each' /opt/lib/feed.rb:243:inrun’
/opt/lib/schedule_runner.rb:159:in create_feed_process' /opt/lib/schedule_runner.rb:154:infork’
/opt/lib/schedule_runner.rb:154:in create_feed_process' /opt/lib/schedule_runner.rb:325:inrun_iteration’
/opt/lib/schedule_runner.rb:269:in run' /opt/lib/schedule_runner.rb:268:inloop’
/opt/lib/schedule_runner.rb:268:in `run’
bin/run_feed:57

data = nil

data.each do |element|
puts element
end

–output:–
Line 3: undefined method `each’ for nil:NilClass (NoMethodError)

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

data = nil
my_method(data)

–output:–
Line 2:in my_method': undefined methodeach’ for nil:NilClass
(NoMethodError)
from t.rb:9

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 methodeach’ for nil:NilClass
(NoMethodError)
from t.rb:15

That is the nature of your error.

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.

Thank you both very much. This helps immensely.

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.