# NoMethodError (undefined method \`\[\]' for nil:NilClass)

**URL:** <https://www.ruby-forum.com/t/nomethoderror-undefined-method-for-nil-nilclass/173024>\
**Category:** Rails\
**Created:** [July 31, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/nomethoderror-undefined-method-for-nil-nilclass/173024 "2009-07-31T15:12:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecae2f/32.png) [@seenu](https://www.ruby-forum.com/u/seenu)\
**Post date:** [July 31, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/nomethoderror-undefined-method-for-nil-nilclass/173024/1 "2009-07-31T15:12:48Z")

</div>

Hi everyone this is the error that’s been killing me, please tell  
where i am missing.  
I have upgraded my rails version from 1.2.3 to 2.3.2 and have  
installed active\_scaffold plugin, since then this error started which  
was not there in older version.

This is the log output:

Processing AccountController#consumer\_home (for 122.167.8.236 at  
2009-07-31 09:02:22) [GET]

NoMethodError (undefined method `[]' for nil:NilClass): app/controllers/account_controller.rb:566:in`paginate\_by\_sql’  
app/controllers/account\_controller.rb:623:in `consumer\_home’  
Rendering /home/v8354a/public\_html/projectbidding/public/500.html (500  
Internal Server Error)

I have paginate\_by\_sql function in application\_controller.rb file  
which is shown below will call Paginator class.

def paginate\_by\_sql(model, sql, per\_page, options={})  
if options[:count]  
if options[:count].is\_a? Integer  
total = options[:count]  
else  
total = model.count\_by\_sql(options[:count])  
end  
else  
total = model.count\_by\_sql\_wrapping\_select\_query(sql)  
end  
object\_pages = Paginator.new self, total, per\_page, @params  
[‘page’]  
objects = model.find\_by\_sql\_with\_limit(sql,  
object\_pages.current.to\_sql[1], per\_page)  
return [object\_pages, objects]  
end

Please help

---

<div class="post-metadata">

**Author:** ![seenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecae2f/32.png) [@seenu](https://www.ruby-forum.com/u/seenu)\
**Post date:** [July 31, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/nomethoderror-undefined-method-for-nil-nilclass/173024/2 "2009-07-31T15:19:49Z")

</div>

2009/7/31 seenu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 2009-07-31 09:02:22) [GET]  
> def paginate\_by\_sql(model, sql, per\_page, options={})  
> [‘page’]  
> Â Â Â Â Â Â objects = model.find\_by\_sql\_with\_limit(sql,  
> object\_pages.current.to\_sql[1], per\_page)  
> Â Â Â Â Â Â return [object\_pages, objects]  
> Â Â Â Â end

You have not told us which is line 566 which is where the error is.  
Looking at the code though I guess it is one of the lines containing  
options[:count] or maybe @params[‘page’]. The error is undefined  
method `[]’ for nil:NilClass which would suggest that maybe options or  
params is not set. If you can’t see the problem by inspecting the  
code you could try using ruby-debug to break in at the appropriate  
point and have a look at what is going on. The rails guide on  
testing applications is good.

Colin

---

<div class="post-metadata">

**Author:** ![seenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecae2f/32.png) [@seenu](https://www.ruby-forum.com/u/seenu)\
**Post date:** [July 31, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/nomethoderror-undefined-method-for-nil-nilclass/173024/3 "2009-07-31T15:28:01Z")

</div>

this is the call i am making on line 566:  
@auction\_pages = paginate\_by\_sql (Auction, auctions\_query, 10, ‘’)

---

<div class="post-metadata">

**Author:** ![seenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecae2f/32.png) [@seenu](https://www.ruby-forum.com/u/seenu)\
**Post date:** [July 31, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/nomethoderror-undefined-method-for-nil-nilclass/173024/4 "2009-07-31T15:39:44Z")

</div>

2009/7/31 seenu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> this is the call i am making on line 566:  
> @auction\_pages = paginate\_by\_sql (Auction, auctions\_query, 10, ‘’)

Are you sure? From the trace I would have expected line 566 of  
account\_controller.rb to have been in the function paginate\_by\_sql and  
the call to paginate\_by\_sql to be at line 623 (in consumer\_home).

Whatever the answer to that however, I suggest you have a look at the  
last parameter of the call, is that an empty string? The function  
appears to be expecting a hash. Though I am not sure whether that  
would give the error seen.

Did you try using the debugger?

By the way I believe that top posting is rather frowned on in this list.

Colin
