# Nil error?

**URL:** <https://www.ruby-forum.com/t/nil-error/129324>\
**Category:** Rails\
**Created:** [February 16, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/nil-error/129324 "2008-02-16T19:14:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [February 16, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/nil-error/129324/1 "2008-02-16T19:14:23Z")

</div>

Hi everyone,

I’m sure I’m doing something stupid with this, so if somebody could talk  
some sense into me that would be great 🙂

I’m trying to just increment a variable and I think I’m missing some  
basic concept (this is RoR 2.0.2 btw)

So have a “players” table with several variables, including one called  
“odds”. I’m using the basic scaffold generator for the CRUD stuff, but  
in my model I have:

attr\_reader :odds

def initialize(odds)  
@odds = odds  
end

def winGame  
@odds += 1  
end

(note: attr\_reader makes the odds in the index list disappear from  
view.)

in the controller I have:

def winner  
player = Player.find(params[:id])  
player.winGame

```
respond_to do |format|
  format.html { redirect_to(players_url) }
  format.xml { head :ok }
end

```

end

and in the view I have:

\<%= link\_to 'Win Game', :action =\> "winner", :id =\> player %\>

This results in:  
You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.+

I can view the correct odds number by using ‘puts odds.to\_s’ but it  
won’t let me set it at any point. Am I missing something?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [February 17, 2008, 5:13am UTC](https://www.ruby-forum.com/t/nil-error/129324/2 "2008-02-17T05:13:23Z")

</div>

Gary H. wrote:

> Hi everyone,
> 
> I’m sure I’m doing something stupid with this, so if somebody could talk  
> some sense into me that would be great 🙂
> 
> I’m trying to just increment a variable and I think I’m missing some  
> basic concept (this is RoR 2.0.2 btw)
> 
> So have a “players” table with several variables, including one called  
> “odds”. I’m using the basic scaffold generator for the CRUD stuff, but  
> in my model I have:
> 
> attr\_reader :odds
> 
> def initialize(odds)  
> @odds = odds  
> end
> 
> This results in:  
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.+
> 
> I can view the correct odds number by using ‘puts odds.to\_s’ but it  
> won’t let me set it at any point. Am I missing something?
> 
> Thanks in advance!

This is just a guess, but I would try replacing attr\_reader with  
attr\_accessor. It may be that since you’re not telling ruby to make a  
method for setting odds, that it is unable to set it via the @odds  
variable.

I’m no expert, but I play one on tv.

cheers,  
jp

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [February 18, 2008, 12:57am UTC](https://www.ruby-forum.com/t/nil-error/129324/3 "2008-02-18T00:57:51Z")

</div>

The attributes that Rails creates by reading the content\_columns from  
a db table are not instance variables as in a traditional Ruby class.  
They are actually all stored in an internal hash called attributes,  
and Rails does some method\_missing magic to create accessors for you.  
By creating the accessor for odds as you have, you’ve effectively  
aliased the db column.

Instead, you can initialize your player object like this:

Player.new(:odds=\>…)

and your win\_game method:

def win\_game  
self.odds += 1 (or update\_attributes(:odds, self.odds+1) if you want  
to persist it immediately)  
end

HTH,  
AndyV

On Feb 16, 1:14 pm, “Gary H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
