Forum: Ruby on Rails nil object when you didn't expect it

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alana (Guest)
on 2006-04-20 19:55
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:    </font>
22:    </td>
23:    <td>
24:      <%= link_to registeredphone.user.txtForename,
25: 		 :action => "list",
26: 		 :user => "#{registeredphone.user.txtForename}" %>
27:    </td>


Thanks, Alana
Bill W. (Guest)
on 2006-04-20 20:06
(Received via mailing list)
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>
To: <removed_email_address@domain.invalid>
Sent: Thursday, April 20, 2006 10:55 AM
Subject: [Rails] nil object when you didn't expect it
Alana (Guest)
on 2006-04-20 20:18
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) %>
<table border="1">
 <tr>
  <td width="30%"><p align="center"><i><b>Phone Number</b></i></td>
  <td width="20%"><p align="center"><i><b>User</b></i></td>
  <td width="50%"><p align="center"><i><b>Registration Date and
Time</b></i></td>
 </tr>

 <% @registeredphones.each do |registeredphone| %>
 <% if (@user == nil) || (@user == registeredphone.user.txtForename)%>
  <tr>
   <td>
   <%= link_to registeredphone.txtPhoneNumber,
		:action => "show",
		:id => registeredphone.id %>
   <font size=-1>

   <%= link_to "(delete)",
		{:action => "delete", :id => registeredphone.id},
		:confirm => "Are you sure you want to delete
#{registeredphone.txtPhoneNumber}?" %>
   </font>
   </td>
   <td>
     <%= link_to registeredphone.user.txtForename,
		 :action => "list",
		 :user => "#{registeredphone.user.txtForename}" %>
   </td>
   <td>
     <%= registeredphone.txtRegisterDateTime %>
   </td>
  </tr>
 <% end %>
 <% end %>
</table>


Thanks,

Alana
Bill W. (Guest)
on 2006-04-20 20:49
(Received via mailing list)
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>
To: <removed_email_address@domain.invalid>
Sent: Thursday, April 20, 2006 11:18 AM
Subject: [Rails] Re: nil object when you didn't expect it
Adam B. (Guest)
on 2006-04-20 23:14
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?
Bojan M. (Guest)
on 2006-04-22 01:11
(Received via mailing list)
Alana wrote:
> You have a nil object when you didn't expect it!
> 27:    </td>
>
>
> 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
-> tools, scripts, tricks from our code lab: http://source.mihelac.org
This topic is locked and can not be replied to.