# Nil object when you didn't expect it

**URL:** <https://www.ruby-forum.com/t/nil-object-when-you-didnt-expect-it/58335>\
**Category:** Rails\
**Created:** [April 20, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/nil-object-when-you-didnt-expect-it/58335 "2006-04-20T17:55:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 20, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/nil-object-when-you-didnt-expect-it/58335/1 "2006-04-20T17:55:50Z")

</div>

Hi,

I’m new to rails and finding it hard to figure out what this error is  
referring to. Can anyone help please?!!

NoMethodError in Registeredphone#list

Showing app/views/registeredphone/list.rhtml where line #24 raised:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.txtForename

Extracted source (around line #24):

21:   
22:   
23:

  
24: \<%= link\_to registeredphone.user.txtForename,  
25: :action =\> “list”,  
26: :user =\> “#{registeredphone.user.txtForename}” %\>  
27: 

Thanks, Alana

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 20, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/nil-object-when-you-didnt-expect-it/58335/2 "2006-04-20T18:06:58Z")

</div>

Hi Alana,

It’s saying that in evaluating registeredphone.user.txtForename,  
registeredphone.user has a nil value. In order to have a value in the  
view,  
it would have to have been passed in by the controller.

hth,  
Bill

----- Original Message -----  
From: “Alana” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, April 20, 2006 10:55 AM  
Subject: [Rails] nil object when you didn’t expect it

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 20, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/nil-object-when-you-didnt-expect-it/58335/3 "2006-04-20T18:18:28Z")

</div>

Hi Bill,

Thanks for your reply. I still can’t figure out the error! Can you offer  
any words of wisdom?!

This is my controller for registeredphones:

class RegisteredphoneController \< ApplicationController  
layout “standard-layout”  
scaffold :registeredphone

```
def delete
	Registeredphone.find(@params['id']).destroy
	redirect_to :action => 'list'
end

def create
	@registeredphone = Registeredphone.new(@params['registeredphone'])
	@registeredphone.txtregisterdatetime = Time.now
	if @registeredphone.save
		redirect_to :action => 'list'
	else
		render_action 'new'
	end
end

def new
	@registeredphone = Registeredphone.new
	@user = User.find_all
end

def list
	@user = @params['user']
	@registeredphones = Registeredphone.find_all
end

def edit
	@registeredphone = Registeredphone.find(@params["id"])
	@user = User.find_all
end

```

end

This is my list.rhtml for registeredphones:

\<%= debug(@registeredphone) %\>

\<% @registeredphones.each do |registeredphone| %\>  
\<% if (@user == nil) || (@user == registeredphone.user.txtForename)%\>

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

| 

_ **Phone Number** _

 | 

_ **User** _

 | 

_ **Registration Date and Time** _

 |
| \<%= link\_to registeredphone.txtPhoneNumber, :action =\> "show", :id =\> registeredphone.id %\> 

\<%= link\_to “(delete)”,  
{:action =\> “delete”, :id =\> registeredphone.id},  
:confirm =\> “Are you sure you want to delete  
#{registeredphone.txtPhoneNumber}?” %\>

 | \<%= link\_to registeredphone.user.txtForename, :action =\> "list", :user =\> "#{registeredphone.user.txtForename}" %\> | \<%= registeredphone.txtRegisterDateTime %\> |

Thanks,

Alana

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 20, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/nil-object-when-you-didnt-expect-it/58335/4 "2006-04-20T21:14:55Z")

</div>

Because it gets past this line:

registeredphone.txtPhoneNumber

registeredphone is not a nil object, since it fails on this line:

registeredphone.user.txtForename

registeredphone.user is a nil object. Are you absolutely positive you  
assigned a user when this phone was created?

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 20, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/nil-object-when-you-didnt-expect-it/58335/5 "2006-04-20T18:49:58Z")

</div>

Hi Alana,

I’m not sure exactly what you’re trying to accomplish, and I’m still  
pretty  
new to RoR, but I’ll give it a shot. From your ‘edit’ action, it looks  
like  
you have two tables: User and Registeredphone. It looks, in your  
‘list.rhtml’, like what you’re trying to get ready to do is list all the  
phones for a particular user. If that’s the case, you need to modify  
the  
‘list’ action in your controller so that that’s what you’re passing in  
to  
the view. Right now, you’re passing in the entire list of Registered  
phones  
and the user and it looks like you’re trying to do the database join  
work in  
the view. At any rate, without additional info, that’s how I interpret  
the  
registeredphone.user.txtForename usage.

Is there some reason you can’t just use one table? That could simplify  
things for you.

hth,  
Bill  
----- Original Message -----  
From: “Alana” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, April 20, 2006 11:18 AM  
Subject: [Rails] Re: nil object when you didn’t expect it

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 21, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/nil-object-when-you-didnt-expect-it/58335/6 "2006-04-21T23:11:24Z")

</div>

Alana wrote:

> You have a nil object when you didn’t expect it!  
> 27:
> 
> Thanks, Alana

\<%= link\_to registeredphone.user.txtForename, :action =\> “list”, :user  
=\> “#{registeredphone.user.txtForename}” if registeredphone.user %\>

would do the trick, even if the user is nil

hope it helps,  
Bojan

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)
