# Stuck on silly nil issue

**URL:** <https://rubytalk.org/t/stuck-on-silly-nil-issue/55065>\
**Category:** ruby-talk\
**Created:** [25 August 2009 06:40 UTC](https://rubytalk.org/t/stuck-on-silly-nil-issue/55065 "2009-08-25T06:40:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Wild](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@Nick\_Wild](https://rubytalk.org/u/Nick_Wild)\
**Post date:** [25 August 2009 06:40 UTC](https://rubytalk.org/t/stuck-on-silly-nil-issue/55065/1 "2009-08-25T06:40:22Z")

</div>

Banging my head on the desk over this..

I'll try to generalize my issue down to this scenario. Since I remember  
always using cars back in programming classes here it goes...

cars["yugo"].status["available"] = 2

Some thing like this is working perfectly...

if cars["yugo"].status["available"]  
&nbsp;&nbsp;&nbsp;puts "There are" + cars["yugo"].status["available"].to\_i + "yugos  
available"  
end  
if cars["kia"].status["available"]  
&nbsp;&nbsp;&nbsp;puts "There are" + cars["kia"].status["available"].to\_i + "kia  
available"  
end

I get "There are 2 yugos available"

But when I do this....

cars\_to\_scan = ["yugo", "kia"]  
cars\_to\_scan.each { |carbrand|  
&nbsp;&nbsp;if cars[carbrand].status["available"]  
&nbsp;&nbsp;&nbsp;&nbsp;puts cars["yugo"].status["available"].to\_i + carbrand + "available"  
}

I hit: You have a nil object when you didn't expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.attributes

So I tried

if cars[carbrand.to\_s].status["available"]  
and also this:  
unless cars[carbrand.to\_s].status["available"].nil?

same error

I don't understand why I can use the string directly but when I try  
indirectly it's hitting this error and why I'm getting a nil error when  
I'm trying to use the if to check if the object is not nil.

I'm still really baffled by certain things in ruby. Thanks in advance  
for any tips or pointers. I'm sorry for the newbie question but this is  
one of those dig through google and books and still come up ready to  
pull your hair out little niggling things.

Thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 August 2009 06:59 UTC](https://rubytalk.org/t/stuck-on-silly-nil-issue/55065/2 "2009-08-25T06:59:53Z")

</div>

Look at your line that starts with "puts" - it's just a silly copy and  
paste error. 🙂

Note: if you need to access a single object multiple times in the  
block, it is more efficient and probably also more readable to just  
pull it once.

Cheers

robert

> **···**
>
> 2009/8/25 Nick Wild \<nickwild@gmail.com\>:
> 
> > Banging my head on the desk over this..
> > 
> > I'll try to generalize my issue down to this scenario. Since I remember  
> > always using cars back in programming classes here it goes...
> > 
> > cars["yugo"].status["available"] = 2
> > 
> > Some thing like this is working perfectly...
> > 
> > if cars["yugo"].status["available"]  
> > puts "There are" + cars["yugo"].status["available"].to\_i + "yugos  
> > available"  
> > end  
> > if cars["kia"].status["available"]  
> > puts "There are" + cars["kia"].status["available"].to\_i + "kia  
> > available"  
> > end
> > 
> > I get "There are 2 yugos available"
> > 
> > But when I do this....
> > 
> > cars\_to\_scan = ["yugo", "kia"]  
> > cars\_to\_scan.each { |carbrand|  
> > if cars[carbrand].status["available"]  
> > puts cars["yugo"].status["available"].to\_i + carbrand + "available"  
> > }
> > 
> > I hit: You have a nil object when you didn't expect it!  
> > You might have expected an instance of ActiveRecord::Base.  
> > The error occurred while evaluating nil.attributes
> > 
> > So I tried
> > 
> > if cars[carbrand.to\_s].status["available"]  
> > and also this:  
> > unless cars[carbrand.to\_s].status["available"].nil?
> > 
> > same error
> > 
> > I don't understand why I can use the string directly but when I try  
> > indirectly it's hitting this error and why I'm getting a nil error when  
> > I'm trying to use the if to check if the object is not nil.
> > 
> > I'm still really baffled by certain things in ruby. Thanks in advance  
> > for any tips or pointers. I'm sorry for the newbie question but this is  
> > one of those dig through google and books and still come up ready to  
> > pull your hair out little niggling things.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Nick\_Wild](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@Nick\_Wild](https://rubytalk.org/u/Nick_Wild)\
**Post date:** [25 August 2009 07:17 UTC](https://rubytalk.org/t/stuck-on-silly-nil-issue/55065/3 "2009-08-25T07:17:54Z")

</div>

Robert - Thanks for taking a look!

Maybe I should have used my actual code instead of trying to write up an  
example. The lack of end on my if in the example wasn't the issue.  
Here's the actual block with content sanitized.

Where xmldoc is a REXML object I'm using without problem elsewhere in  
this code.

The IF line below is where I hit the problems

types = ["foo", "bar", "stuff", "other", "favorite"]  
conditions = ["up", "down", "marginal"]

types.each { |@scan\_type|  
&nbsp;&nbsp;conditions.each { |@scan\_conditions|  
&nbsp;&nbsp;&nbsp;if xmldoc.elements[@scan\_type].attributes[@scan\_conditions]  
&nbsp;&nbsp;&nbsp;&nbsp;count\_to\_add =  
site.elements[@scan\_type].attributes[@scan\_conditions].to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;addtodb(siteToSave.id, scan\_type, scan\_conditions,count\_to\_add)  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;}  
}

Robert Klemme wrote:

> **···**
>
> > 2009/8/25 Nick Wild \<nickwild@gmail.com\>:
> > 
> > > same error
> > > 
> > > I don't understand why I can use the string directly but when I try  
> > > indirectly it's hitting this error and why I'm getting a nil error when  
> > > I'm trying to use the if to check if the object is not nil.
> > > 
> > > I'm still really baffled by certain things in ruby. Thanks in advance  
> > > for any tips or pointers. I'm sorry for the newbie question but this is  
> > > one of those dig through google and books and still come up ready to  
> > > pull your hair out little niggling things.
> > 
> > Look at your line that starts with "puts" - it's just a silly copy and  
> > paste error. 🙂
> > 
> > Note: if you need to access a single object multiple times in the  
> > block, it is more efficient and probably also more readable to just  
> > pull it once.
> > 
> > Cheers
> > 
> > robert
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 August 2009 07:24 UTC](https://rubytalk.org/t/stuck-on-silly-nil-issue/55065/4 "2009-08-25T07:24:33Z")

</div>

Nick, please do not top post.

> Robert - Thanks for taking a look!
> 
> Maybe I should have used my actual code instead of trying to write up an  
> example. The lack of end on my if in the example wasn't the issue.

I actually meant the use of a constant vs. the local variable from the  
iteration:

puts cars["yugo"] ...

> Here's the actual block with content sanitized.
> 
> Where xmldoc is a REXML object I'm using without problem elsewhere in  
> this code.
> 
> The IF line below is where I hit the problems
> 
> types = ["foo", "bar", "stuff", "other", "favorite"]  
> conditions = ["up", "down", "marginal"]
> 
> types.each { |@scan\_type|  
> conditions.each { |@scan\_conditions|  
> if xmldoc.elements[@scan\_type].attributes[@scan\_conditions]  
> count\_to\_add =  
> site.elements[@scan\_type].attributes[@scan\_conditions].to\_i  
> addtodb(siteToSave.id, scan\_type, scan\_conditions,count\_to\_add)  
> end  
> }  
> }

You are abusing instance variables. Please get rid of all the @'s as  
they have no use in this case.

Then, #elements returns a collection of elements - you need to iterate  
them if you want to get at each elements attributes. I believe though  
that there are XPath expressions which directly select attributes. I  
am unsure whether REXML supports them. See:

[http://www.w3schools.com/xpath/](http://www.w3schools.com/xpath/)  
[http://www.zvon.org/xxl/XPathTutorial/General/examples.html](http://www.zvon.org/xxl/XPathTutorial/General/examples.html)

My previous remark about not fetching things twice is even more valid  
with REXML. By refactoring to only fetch certain things once your  
code will become more readable and more efficient.

Kind regards

robert

> **···**
>
> 2009/8/25 Nick Wild \<nickwild@gmail.com\>:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Nick\_Wild](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@Nick\_Wild](https://rubytalk.org/u/Nick_Wild)\
**Post date:** [25 August 2009 07:42 UTC](https://rubytalk.org/t/stuck-on-silly-nil-issue/55065/5 "2009-08-25T07:42:06Z")

</div>

Hopefully this will not top post.

Yes, sorry my example was so hackish I was churning a bunch of code  
trying to zero in on the issue. I'll switch over to iterating through  
with a single fetch per pass and shed the ivars.

For what it is worth I did just figure out that when I test if  
xmldoc.elements[@scan\_type] is nil first this code works albeit  
inefficiently as noted. I was foolishly thinking that nil.nil would eval  
as false for the if.

Thank you again for your advice.

Cheers,

Nick

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
