# Problem - getting a nil object in view?

**URL:** <https://www.ruby-forum.com/t/problem-getting-a-nil-object-in-view/160830>\
**Category:** Rails\
**Created:** [February 20, 2009, 4:52am UTC](https://www.ruby-forum.com/t/problem-getting-a-nil-object-in-view/160830 "2009-02-20T04:52:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [February 20, 2009, 4:52am UTC](https://www.ruby-forum.com/t/problem-getting-a-nil-object-in-view/160830/1 "2009-02-20T04:52:59Z")

</div>

Hello All,

I am new to ruby programming,  
Problem is -

In my controller -

I defined a private method like  
:private  
def \_list(searchBy, moreResults = false)

# another code goes here…

@items\_by\_language = {}  
@items.each do |item|  
@items\_by\_language[item.language]=[] if  
@items\_by\_language[item.language] == nil  
@items\_by\_language[item.language] \<\< item  
end

end

# This @items\_by\_language is a hash which I am accessing in my view

named list.html.erb But I get following error  
NoMethodError in Home#list

Showing home/list.html.erb where line #6 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

Extracted source (around line #6):

list.html.erb code -----------------------

\<%@items\_by\_language.each do |key,val|%\>  
\<%val.each do |items|%\>

//////code performed on @items\_by\_language hash variable

## \<%end%\> \<%end%\>

In controller “\_list method” is private and I want it private . Thats  
why it is not allowed me to access @items\_by\_language.

What should I do?

Thanks,  
Vikas

---

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [February 20, 2009, 10:48am UTC](https://www.ruby-forum.com/t/problem-getting-a-nil-object-in-view/160830/2 "2009-02-20T10:48:04Z")

</div>

On Feb 20, 3:52 am, Vikas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In controller “\_list method” is private and I want it private . Thats  
> why it is not allowed me to access @items\_by\_language.

method visibility has nothing to do with instance variables. If you  
are going to localhost:3000/some\_controller/list then your \_list  
method is probably not even being called.

Fred
