Newbie .. nil object and missing something obvious


#1

Trying to do first project in Ruby on Rails, and I’m getting the dreaded
nil object exception. I’ve reduced the code to what I think is the
minimal case, and it’s still not working. Code presented below…

in “app/controllers/dashboard_controller.rb”

class DashboardController < ApplicationController
def display_calendar
@dates[0] = Date.Today
@dates << Date.Today + 1
end
end

in “app/views/dashboard/display_calendar.rhtml”

Display Calendar

First day should be <%= Date.today %>
Second day should be <%= Date.today + 1 %> First day is <%= @dates[0] %>

-------------------------------------------------------------------

The fact that the page is getting a nil suggests that the controller is
not setting @dates properly. However, the code is so simple that I’m
sure I’m missing something fundamental. Any suggestions?

Thanks in advance for your help.


#2

Robert Sherwood wrote:

Trying to do first project in Ruby on Rails, and I’m getting the dreaded
nil object exception.

What happens if you change ‘@dates[0] = Date.Today’ to ‘@dates =
Date.Today’?

-Lindsay


#3

What happens if you change ‘@dates[0] = Date.Today’ to ‘@dates =
Date.Today’?

Thanks for you reply. I have tried that variation, but I’m getting the
same error.

In the other statement, will @dates still be an array, and will
@dates[0] be meaningful? How does Ruby know that it is building @dates
as an array of date objects, rather than a single Date?


#4

I realized it would be helpful to print the entire error:


Showing app/views/dashboard/display_calendar.rhtml where line #6 raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]

Extracted source (around line #6):

3:


4: First day should be <%= Date.today %>

5: Second day should be <%= Date.today + 1 %>
6: First day is <%= @dates[0] %>
7:

RAILS_ROOT: script/…/config/…


#5

Robert Sherwood wrote:

The fact that the page is getting a nil suggests that the controller is
not setting @dates properly. However, the code is so simple that I’m
sure I’m missing something fundamental. Any suggestions?

Thanks in advance for your help.

irb(main):001:0> x = Date.Today
NoMethodError: undefined method `Today’ for Date:Class
from (irb):1
irb(main):002:0> x = Date.today
=> #<Date: 4907697/2,0,2299161>
irb(main):003:0>

Jeff C.man


#6

Can you post the exact error message you are getting?

Also, in your example, you use ‘Date.Today’ and ‘Date.today’ (note the
capitalization). That could cause problems if it isn’t just a typo.

You can also put ‘puts @dates[0]’ in the controller and the message
should show up on your WEBrick console.

Try the display_calendar function in irb to make sure it is doing what
you think it should.

on Sunday, April 23, 2006, at 9:58 PM, Robert Sherwood wrote:

end

_______________________________________________ Rails mailing list removed_email_address@domain.invalid http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#7

That helps some…

@dates does not exist for some reason.

I think you need to initialize @dates as an array before you use it…
try this…

def display_calendar
@dates = [Date.today, Date.today+1]
end

On Sunday, April 23, 2006, at 10:48 PM, Robert Sherwood wrote:

Extracted source (around line #6):

Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#8

On Apr 23, 2006, at 12:58 PM, Robert Sherwood wrote:

First day should be <%= Date.today %>
Second day should be <%= Date.today + 1 %> First day is <%= @dates[0] %>

The other folks solved your problem, but I’ll
throw in a style tip:

First day should be <%= Date.today %>
Second day should be <%= Date.today + 1 %> First day is <%= @dates.first %> Last day is <%= @dates.last %>

Additionally, rails provides some really nice
date routines.

1.day.from_now


– Tom M.


#9

Thanks so much for everyone’s help. Lot’s of good advice. I’m still
having the problem, but I appreciate everyone’s patience so far.

Let me print the new code and error:

app/controllers/dashboard_controller.rb

class DashboardController < ApplicationController
def display_calendar
@dates = [Date.today, Date.today + 1]
end
end

app/views/dashboard/display_calendar.rhtml

Display Calendar

First day should be <%= Date.today %>
Second day should be <%= Date.today + 1 %>
First day is <%= @dates.first %>
Last day is <%= @dates.last %>


Error page:

NoMethodError in Dashboard#display_calendar.rhtml

Showing app/views/dashboard/display_calendar.rhtml where line #6 raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.first

Extracted source (around line #6):

3:


4: First day should be <%= Date.today %>

5: Second day should be <%= Date.today + 1 %>

6: First day is <%= @dates.first %>

7: Last day is <%= @dates.last %>
8:

As an earlier poster suggested, it looks like @dates isn’t being
initialized. What’s driving me crazy is that the code is so simple, I
don’t understand what can be going wrong. It’s a simple assignment and
display. At another poster’s suggestion, I have run the code through
irb:

irb(main):015:0> dates = [Date.today, Date.today + 1]
=> [#<Date: 4907697/2,0,2299161>, #<Date: 4907699/2,0,2299161>]
irb(main):016:0> puts dates[0]
2006-04-23
=> nil
irb(main):017:0> puts dates[1]
2006-04-24
=> nil
irb(main):018:0> puts dates.first
2006-04-23
=> nil
irb(main):019:0> puts dates.last
2006-04-24
=> nil

Of course, the differnce here is that not using an instance variable.
Could that be the problem? I’m sure I’ve seen instance variables used
for this purpose in other code.

In any case. Thanks to everyone for your help so far…


#10

Kevin O. wrote:

Hmmm… now this is getting weird. Try putting a ‘<%= @dates.inspect
%>’ in there.
Changes listed below:


app/views/dashboard/display_calendar.rhtml

Display Calendar

First day should be <%= Date.today %>
Second day should be <%= Date.today + 1 %>
<%= @dates.inspect %>


Output:

Display Calendar

First day should be 2006-04-23
Second day should be 2006-04-24
nil

What version of rails/ruby are you running?
$ ruby --version
ruby 1.8.4 (2005-12-24) [i486-linux]

$ rails --version
Rails 1.1.2

Thanks again to everyone for the help. I’m going to try regenerating the
project with the rails command, adding just those two files, and
reloading…


#11

Thanks again to everyone for the help. I’m going to try regenerating the
project with the rails command, adding just those two files, and
reloading…

Okay, that didn’t work. However, I’ve tried some of the code on the
console. Of course, it works there…

@dates = [Date.today, Date.today + 1]
=> [#<Date: 4907697/2,0,2299161>, #<Date: 4907699/2,0,2299161>]

@dates.first
=> #<Date: 4907697/2,0,2299161>

puts @dates.first
2006-04-23
=> nil

puts @dates.last
2006-04-24
=> nil

Crazymaking!


#12

Hmmm… now this is getting weird. Try putting a ‘<%= @dates.inspect
%>’ in there.

What version of rails/ruby are you running?

On Monday, April 24, 2006, at 3:13 AM, Robert Sherwood wrote:

end
Last day is <%= @dates.last %>
You might have expected an instance of Array.

2006-04-23

removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#13

Kevin, thank you for your help…

Try putting a ‘puts @dates.inspect’ into the controller action at the
end. Then check your WEBrick console window after the request to see
what it outputs.

Okay. Nothing listed in the output of the Console. Just for S+G’s I
added a “puts ‘Testing’” to check. I also added a bogus variable to the
.rhtml file and tried again. The bogus variable (@nothing) is also nil.
Finally, I commented out the entire “display_calendar” method in the
controller, and I got the same output, rather than the usual “missing
method” or “no method responds to <blah, blah…>”

So my question is this. Is it possible that control is not being routed
through the dashboard method? How can I confirm that… also, why does
rails hate me so much :)? I’m going to start digging through the
dispatcher code (I think that’s what it’s called).

My only other suggestion at this point would be to rename @dates to
something like @array1.

I thought maybe dates was some undocumented reserved word, so a couple
of days ago I tried @xyz… no dice.


#14

Okay, started a new application with “rails Test_site”. Created new
controller with “./scripts/generate controller dashboard
diplay_calendar” Copied text from previously buggy code into newly
generated controller. Still having the same problem… I’m off to bed,
Thanks again to everyone who has helped.


#15

What URL are you accessing during testing?


– Tom M.


#16

Try putting a ‘puts @dates.inspect’ into the controller action at the
end. Then check your WEBrick console window after the request to see
what it outputs.

My only other suggestion at this point would be to rename @dates to
something like @array1.

On Monday, April 24, 2006, at 3:45 AM, Robert Sherwood wrote:

=> #<Date: 4907697/2,0,2299161>

Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#17
  1. Make sure there are no other web servers running
  2. make sure you are running in the ‘development’ environment
  3. check your routes.rb file for problems. Your server may be serving
    up the .rhtml file directly
  4. make sure you test it with WEBrick or Mongrel. Disable apache if it
    is running.

As for why Rails hates you… my guess is that she smells XML on your
breath and she thinks you’ve been cheating on her with Java. :wink:

On Monday, April 24, 2006, at 4:39 AM, Robert Sherwood wrote:

controller, and I got the same output, rather than the usual "missing

I thought maybe dates was some undocumented reserved word, so a couple
of days ago I tried @xyz… no dice.


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#18

Robert Sherwood wrote:

Tom M. wrote:

What URL are you accessing during testing?

Excellent sleuthing, Tom!

http://localhost:3000/dashboard/display_calendar.rhtml

Robert,

In Rails, your URL should point to the controller action, not the view
it
renders. Drop the “.rhtml”.

hth,
Bill


#19

On Apr 23, 2006, at 9:14 PM, Bill W. wrote:

Robert Sherwood wrote:

Tom M. wrote:

What URL are you accessing during testing?

Excellent sleuthing, Tom!

Thanks.

http://localhost:3000/dashboard/display_calendar.rhtml

Robert,

In Rails, your URL should point to the controller action, not the
view it renders. Drop the “.rhtml”.

Learn something new every day!

Why is rails trying to serve up .rhtml files if they’re not under
public?

I think this requires a bug report to be filed!


– Tom M.


#20

Tom M. wrote:

What URL are you accessing during testing?

http://localhost:3000/dashboard/display_calendar.rhtml

Thanks