# You have a nil object when you didn't expect it!

**URL:** <https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/113524>\
**Category:** Rails\
**Created:** [September 1, 2007, 4:28am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/113524 "2007-09-01T04:28:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kedron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kedron](https://www.ruby-forum.com/u/Kedron)\
**Post date:** [September 1, 2007, 4:28am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/113524/1 "2007-09-01T04:28:00Z")

</div>

I’ve got a list of members in a sidebar using the code below. It works  
great for the list page, but when I edit or add a new member I get  
“You have a nil object when you didn’t expect it!” - I have a hunch  
this is just a nooby mistake, but I’m at a loss. Any help would be  
greatly appreciated!

\<% for member in @members %\>

- \<%= link\_to h(member.first\_name) + " " + h(member.last\_name), edit\_member\_path(player) %\>
\<% end %\>

---

<div class="post-metadata">

**Author:** ![Kedron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kedron](https://www.ruby-forum.com/u/Kedron)\
**Post date:** [September 1, 2007, 4:33am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/113524/2 "2007-09-01T04:33:01Z")

</div>

Kedron -

On 31-Aug-07, at 10:27 PM, Kedron wrote:

> \<% end %\>

Your log file will tell you more - like the line number (it’s a stack  
trace).

generally one of your variables is a nil. The line number should help.

You could try logging the values out. Some people would use the built  
in debugger to help out.

cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![Kedron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kedron](https://www.ruby-forum.com/u/Kedron)\
**Post date:** [September 1, 2007, 4:46am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/113524/3 "2007-09-01T04:46:02Z")

</div>

On Aug 31, 10:32 pm, Jodi S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > greatly appreciated!
> 
> You could try logging the values out. Some people would use the built  
> in debugger to help out.
> 
> cheers,  
> Jodi

Thanks Jodi for the quick response!

The log says its the first line:

\<% for player in @players %\>

---

<div class="post-metadata">

**Author:** ![Kedron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kedron](https://www.ruby-forum.com/u/Kedron)\
**Post date:** [September 1, 2007, 5:00am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/113524/4 "2007-09-01T05:00:04Z")

</div>

Kedron -

On 31-Aug-07, at 10:44 PM, Kedron wrote:

> > > I’ve got a list of members in a sidebar using the code below. It
> > 
> > Jodi
> 
> Thanks Jodi for the quick response!
> 
> The log says its the first line:
> 
> \<% for player in @players %\>

If that’s the case, then it can only be @players

Your controller action for this view must set this collection. Go  
look at the action and verify that @players is set to a legitimate  
value.

Cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![Kedron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kedron](https://www.ruby-forum.com/u/Kedron)\
**Post date:** [September 1, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/113524/5 "2007-09-01T22:06:23Z")

</div>

or check if its nil before using it, so it doesnt throw any exception  
again 🙂

either this or that
