Nil object when you didn't expect it

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

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” [email protected]
To: [email protected]
Sent: Thursday, April 20, 2006 10:55 AM
Subject: [Rails] nil object when you didn’t expect it

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

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?

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” [email protected]
To: [email protected]
Sent: Thursday, April 20, 2006 11:18 AM
Subject: [Rails] Re: nil object when you didn’t expect it

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
→ tools, scripts, tricks from our code lab: http://source.mihelac.org