# nil object Error

**URL:** <https://discuss.rubyonrails.org/t/nil-object-error/62020>\
**Category:** rubyonrails-talk\
**Created:** [December 16, 2011, 7:54am UTC](https://discuss.rubyonrails.org/t/nil-object-error/62020 "2011-12-16T07:54:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachin2](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@sachin2](https://discuss.rubyonrails.org/u/sachin2)\
**Post date:** [December 16, 2011, 7:54am UTC](https://discuss.rubyonrails.org/t/nil-object-error/62020/1 "2011-12-16T07:54:48Z")

</div>

hi all, i am getting following error ,i can’t figure it out why and from where it is coming ? please help me out to resolved this error if anyone knows about this issue. i am using following env.

O.S:Windows XP Ruby:ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32] Rails:Rails 2.3.11 Gem:1.6.2

ActionView::TemplateError (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.) on line #61 of app/views/layouts/main\_bk\_layout.rhtml: 58: \<%if @session != nil%\> 59: 60: \<%else%\> 61: \<%if @session[‘user’].first\_name == “admin”%\> 62:

63: \<%end%\> 64: \<%end%\>

main\_bk\_layout.rhtml is main rhtml loaded.

thanks in advanced !!!

Regards Sachin S. Kewale

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [December 16, 2011, 8:33am UTC](https://discuss.rubyonrails.org/t/nil-object-error/62020/2 "2011-12-16T08:33:44Z")

</div>

> hi all, i am getting following error ,i can't figure it out why and from where it is coming ?

It would help if you had not cropped the top of the error which tells you \_exactly\_ which line is causing the error :-/

> ActionView::TemplateError (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.) on line #61 of app/views/layouts/main\_bk\_layout.rhtml: 58: \<%if @session != nil%\> 59: \<table width="250" border="0" height="20" align="right" cellspacing="0" cellpadding="0"\> 60: \<%else%\> 61: \<%if @session['user'].first\_name == "admin"%\>

At a guess, I'd say you don't have an element in your @session variable for 'user'; hence the "nil object when you didn't expect it".

But that's just a guess based on the info given...

---

<div class="post-metadata">

**Author:** ![sachin2](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@sachin2](https://discuss.rubyonrails.org/u/sachin2)\
**Post date:** [December 16, 2011, 9:06am UTC](https://discuss.rubyonrails.org/t/nil-object-error/62020/3 "2011-12-16T09:06:44Z")

</div>

hi Michael , this full trace of the error below.i am new in ROR ,so can you tell me how to set session in ROR? and if set session is coming null then can set it?

i give you the brief of my project :- in my project the main rhtml is main\_bk\_layout.rhtml into which the one user\_login.rhtml is called. but when i hitting url on local host it will give the error for nil session for user.

full error trace:-

Processing LoginController#user\_login (for 127.0.0.1 at 2011-12-16 13:40:23) [GET] Rendering template within layouts/main\_bk\_layout Rendering login/user\_login

ActionView::TemplateError (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.) on line #61 of app/views/layouts/main\_bk\_layout.rhtml: 58: \<%if @session != nil%\> 59: 60: \<%else%\> 61: \<%if @session[‘user’].first\_name == “admin”%\> 62:

63: \<%end%\> 64: \<%end%\>

```
app/views/layouts/main_bk_layout.rhtml:61:in `_run_rhtml_app47views47layouts47main_bk_layout46rhtml'
D:/Ruby187/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
D:/Ruby187/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:162:in `start'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:95:in `start'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:92:in `each'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:92:in `start'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:23:in `start'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:82:in `start'
-e:2:in `load'
-e:2

```

Rendered rescues/\_trace (46.9ms) Rendered rescues/\_request\_and\_response (15.6ms) Rendering rescues/layout (internal\_server\_error) [4;36;1mSQL (0.0ms)[0m [0;1mSET SQL\_AUTO\_IS\_NULL=0[0m [4;35;1mCountry Load (0.0ms)[0m [0mSELECT \* FROM `countries` [0m

Processing LoginController#user\_login (for 127.0.0.1 at 2011-12-16 13:40:27) [GET] Rendering template within layouts/main\_bk\_layout Rendering login/user\_login

ActionView::TemplateError (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.) on line #61 of app/views/layouts/main\_bk\_layout.rhtml: 58: \<%if @session != nil%\> 59:

60: \<%else%\> 61: \<%if @session[‘user’].first\_name == “admin”%\> 62: 

63: \<%end%\> 64: \<%end%\>

```
app/views/layouts/main_bk_layout.rhtml:61:in `_run_rhtml_app47views47layouts47main_bk_layout46rhtml'
D:/Ruby187/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
D:/Ruby187/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:162:in `start'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:95:in `start'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:92:in `each'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:92:in `start'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:23:in `start'
D:/Ruby187/lib/ruby/1.8/webrick/server.rb:82:in `start'
-e:2:in `load'
-e:2

```

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 16, 2011, 9:20am UTC](https://discuss.rubyonrails.org/t/nil-object-error/62020/4 "2011-12-16T09:20:04Z")

</div>

I don't know what @session is or is supposed to be, but you've got your if statement back to front - you're trying to access @session['user'] when @session is nil

Fred

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [December 16, 2011, 9:27am UTC](https://discuss.rubyonrails.org/t/nil-object-error/62020/5 "2011-12-16T09:27:59Z")

</div>

> hi Michael ,

Please don't top-post. I can't \*make\* you not do it, but I can decline to reply in future :-/

> this full trace of the error below.i am new in ROR ,so can you tell me how to set session in ROR? and if set session is coming null then can set it?

I have no idea what you mean "set session". You have an instance variable called "@session", which you're using in a way that makes me think it's a hash.

> full error trace:- ActionView::TemplateError (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.) on line #61 of app/views/layouts/main\_bk\_layout.rhtml: 58: \<%if @session != nil%\> 59: \<table width="250" border="0" height="20" align="right" cellspacing="0" cellpadding="0"\> 60: \<%else%\> 61: \<%if @session['user'].first\_name == "admin"%\> 62: \<table width="290" border="0" height="20" align="right" cellspacing="0" cellpadding="0"\> 63: \<%end%\> 64: \<%end%\>

Okay... after a second read through the confusion becomes clear. The error tells you that line 61 is the problem - and shows it is where you're accessing "@session['user']" - but looking at the conditional code, you're checking if @session is \*not\* nil and performing a block in that event, and then in your "else" block (where @session \*is\* nil), you access @session, which obviously crashes because @session is nil.

I think you need to review the logical flow of the code.

HTH
