# The error occurred while evaluating nil

**URL:** <https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil/144489>\
**Category:** Rails\
**Created:** [July 30, 2008, 6:35am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil/144489 "2008-07-30T06:35:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 30, 2008, 6:35am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil/144489/1 "2008-07-30T06:35:13Z")

</div>

Hi  
I ahve a code like  
\<%@attachment=ServiceDeskActivityAttachment.find\_by\_service\_desk\_activity\_id(@sd\_activities[loop\_index].id)  
%\>  
\<% if !@attachment.blank? %\>  
\<%= puts ‘is empty’%\>

Attachment&nbsp;&nbsp;&nbsp;&nbsp; File Name \<% else %\> \<%= puts 'is not empty'%\> No attachment \<% end %\>

But I always get the error  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.url

But I am achecking \<% if !@attachment.blank? %\> …Is it not the right  
way?If \<% if !@attachment.blank? %\> is true how the controll enters in  
the if case above?Should it enter the else case only?Please help

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 30, 2008, 7:04am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil/144489/2 "2008-07-30T07:04:27Z")

</div>

Sijo Kg wrote:

> Hi  
> I ahve a code like  
> \<%@attachment=ServiceDeskActivityAttachment.find\_by\_service\_desk\_activity\_id(@sd\_activities[loop\_index].id)  
> %\>  
> \<% if !@attachment.blank? %\>  
> \<%= puts ‘is empty’%\>
> 
> Attachment&nbsp;&nbsp;&nbsp;&nbsp; File Name \<% else %\> \<%= puts 'is not empty'%\> No attachment \<% end %\>
> 
> But I always get the error  
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.url
> 
> But I am achecking \<% if !@attachment.blank? %\> …Is it not the right  
> way?If \<% if !@attachment.blank? %\> is true how the controll enters in  
> the if case above?Should it enter the else case only?Please help
> 
> Thanks in advance  
> Sijo

Hi Sijo  
nil is entirely diferent from blank.your page showing error as nil  
so try the following  
if @attachment  
--------------  
end

the above condition checks for nil.  
and further if your facing like these problem use script/console and  
give the query check wat value is coming and procede further.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 30, 2008, 7:15am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil/144489/3 "2008-07-30T07:15:08Z")

</div>

Hi  
I have already checked as u said  
if @attachment  
--------------  
end  
But getting the same error

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 30, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil/144489/4 "2008-07-30T21:06:47Z")

</div>

2008/7/30, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 31, 2008, 5:06am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil/144489/5 "2008-07-31T05:06:27Z")

</div>

You’re checking @attachment.blank, but you use  
@attachment.attachment.url. It looks you occasionally duplicated  
“attachment”. Or if you have a method “attachment” under @attachment,  
you should check that for nil also (@attachment.attachment.blank?).

LoKi.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 30, 2008, 7:51am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil/144489/6 "2008-07-30T07:51:08Z")

</div>

The attachment method on attachment is returning nil, not the  
@attachment  
variable itself.
