Reputation: 65
I use Nokogiri gem to parse website with 'each' and 'content' methods. It's complicated parser with several usings of those methods. For some reason at some point task fails with error. Undefined method 'content' for nillclass. And gives me references for all lanes I use those classes in. It's not regular fatal error, If I try to put result of those content it prints result needed but still crashes. Here is error I get:
rake aborted!
undefined method `content' for nil:NilClass
/mnt/shoppe/lib/tasks/parser.rake:50:in `block (6 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:49:in `block (5 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:47:in `block (4 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:27:in `block (3 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:12:in `block (2 levels) in <top (required)>'
I'm new to ruby and RoR. Maybe there some type of error which only crash program if some number of this errors reached? Couldn't find mention of something like this, most likely I don't know the key word to look for. So any help will be useful. Thank You.
Upvotes: 0
Views: 90
Reputation: 7482
Usually this means that your input varies at some point. For example, you're parsing an array of nodes in a loop and trying to access get content from a link, something like this:
<div><a href="">test</a></div>
<div><a href="">test</a></div>
<div></div>
Note the third div. If you're expecting in your code that div
always contains a link, your code will fail on the div without link, since link will be nil
and trying to get content on nil
gives you an error you have.
To workaround it you may use link.try(:content)
instead of calling link.content
directly.
Upvotes: 1
Reputation: 34338
Undefined method 'content' for nil:NilClass.
This error means, at some point in your rake task, when you call something.content
, your something
is nil
.
To avoid this issue, you can call content
using try:
something.try(:content)
That way, your program won't crash if some of the something
s are nil
.
Upvotes: 1