# Nil Object

**URL:** <https://www.ruby-forum.com/t/nil-object/138740>\
**Category:** Rails\
**Created:** [May 27, 2008, 10:59am UTC](https://www.ruby-forum.com/t/nil-object/138740 "2008-05-27T10:59:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [May 27, 2008, 10:59am UTC](https://www.ruby-forum.com/t/nil-object/138740/1 "2008-05-27T10:59:09Z")

</div>

well, im tryin to read an email, which already is a txt file in my  
folder, which name is ForActionMailer, but when i run the command it  
shows this error

C:\rubydev\prac1\mail5\>ruby script/runner  
EventMailer.receive(STDIN.read) \< ForA  
ctionMailer.txt  
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/runner.rb:47:  
C:/rubydev  
/prac1/mail5/app/models/event\_mailer.rb:16:in `receive': You have a nil object w hen you didn't expect it! (NoMethodError) The error occurred while evaluating nil.attachments from C:/rubydev/prac1/ma il5/app/models/event_mailer.rb:15:in `each’  
from C:/rubydev/prac1/mail5/app/models/event\_mailer.rb:15:in  
`receive' from c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.0.2/lib/action_mailer /base.rb:373:in `receive’  
from (eval):1  
from  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in `ev al' from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/runner.rb:4 7 from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `ge  
m\_original\_require’  
from  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in `re  
quire’  
from script/runner:3

require ‘rubygems’  
require ‘action\_mailer’

This is my Action mailer code:

class EventMailer \< ActionMailer::Base

```
def receive(email)
  page = Page.find_by_address(email.to.first)
  page.emails.create(
    :subject => email.subject,
    :body => email.body
  )

  if email.has_attachments?
    for attachment in email.attachments
      page.attachments.create({
        :file => attachment,
        :description => email.subject
      })
    end
  end
end

```

end

and i have this is my migrate folder

class CreateEmails \< ActiveRecord::Migration  
def self.up  
create\_table :emails do |t|  
t.integer :page\_id  
t.string :subject  
t.string :body

```
  t.timestamps
end

```

end

def self.down  
drop\_table :emails  
end  
end

* * *

THIS is the content i wanna read. its the foractionmailer txt file

FFrom aaa Fri May 23 16:36:41 2008  
Delivered-To: removed\_email\_address@domain.invalid  
Received: from [gmail-pop.l.google.com](http://gmail-pop.l.google.com) [111.11.11.111]  
by [xxx.com](http://xxx.com) with POP3 (fetchmail-6.3.8)  
for [aaa@localhost](mailto:aaa@localhost) (single-drop); Fri, 23 May 2008 16:36:41 +0800  
(MPST)  
Received: by 11.111.111.11with SMTP id u16cs17333ebc;  
Tue, 20 May 2008 23:42:23 -0700 (PDT)  
Received: by 11.111.11.11 with SMTP id 2123p.1123123.1211231233039;  
Tue, 20 May 2008 23:42:23 -0700 (PDT)  
Received: from [blu0-omc2-s19.blu0.hotmail.com](http://blu0-omc2-s19.blu0.hotmail.com)  
([blu0-omc2-s19.blu0.hotmail.com](http://blu0-omc2-s19.blu0.hotmail.com) [11.111.11.11])  
by [mx.google.com](http://mx.google.com) with ESMTP id h4432 fh.30.2008.05.20.23.42.22;  
Tue, 20 May 2008 23:42:23 -0700 (PDT)  
Received-SPF: pass ([google.com](http://google.com): domain of removed\_email\_address@domain.invalid designates  
65.55.111.94 as permitted sender) client-ip=65.55.111.94;  
Authentication-Results: [mx.google.com](http://mx.google.com); spf=pass ([google.com](http://google.com): domain of  
removed\_email\_address@domain.invalid designates 11.11.111.11 as permitted sender)  
smtp.mail=removed\_email\_address@domain.invalid  
Received: from Bewr46-W231 ([11.11.111.11]) by  
[blu0-omc2-s19.blu0.hotmail.com](http://blu0-omc2-s19.blu0.hotmail.com) with Microsoft SMTPSVC(6.0.3790.3959);  
Tue, 20 May 2008 23:42:22 -0700  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Return-Path: removed\_email\_address@domain.invalid  
Content-Type: multipart/alternative;  
boundary=“_d1512362-2905-4b45-8a7d-183ee6f27167_”  
X-Originating-IP: [147.243.216.4]  
From: xxx xxx [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: blablabla  
Date: Wed, 21 May 2008 02:42:22 -0400  
Importance: Normal  
MIME-Version: 1.0  
X-OriginalArrivalTime: 21 May 2008 06:42:22.0200 (UTC)  
FILETIME=[D294DF80:01C8BB0D]

–_d1512362-2905-4b45-8a7d-183ee6f27167_  
Content-Type: text/plain; charset=“Windows-1252”  
Content-Transfer-Encoding: quoted-printable

asas

=20

* * *

Make every e-mail and IM count. Join the i=92m Initiative from  
Microsoft.  
[http://im.live.com/Messenger/IM/Join/Default.aspx?source=3DEML\_WL\_](http://im.live.com/Messenger/IM/Join/Default.aspx?source=3DEML_WL_)  
MakeCoun=  
t=

–_d1512362-2905-4b45-8a7d-183ee6f27167_  
Content-Type: text/html; charset=“Windows-1252”  
Content-Transfer-Encoding: quoted-printable

.hmmessage P { margin:0px; padding:0px } body.hmmessage { FONT-SIZE: 10pt; FONT-FAMILY:Tahoma } asas  
  

&nbsp;  
 ![3D"Image]()  

\<= br /\>
* * *
Make every e-mail and IM count. Jo= in the i=92m Initiative from Microsoft. =

–_d1512362-2905-4b45-8a7d-183ee6f27167_–

can some one pls help me~~~  
sry for any problems or inconvinient  
thx~~

cheers~~~

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [May 27, 2008, 11:28am UTC](https://www.ruby-forum.com/t/nil-object/138740/2 "2008-05-27T11:28:24Z")

</div>

On 27 May 2008, at 09:59, Cean O. wrote:

> well, im tryin to read an email, which already is a txt file in my  
> folder, which name is ForActionMailer, but when i run the command it  
> shows this error

Walk through the code with the debugger and work out which object is  
nil (and why).

Fred

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [May 27, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/nil-object/138740/3 "2008-05-27T13:55:46Z")

</div>

fred pls help me as im really new to ruby… but need to rush some  
project i cant really adopt ruby yet…

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [May 27, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/nil-object/138740/4 "2008-05-27T14:07:56Z")

</div>

On 27 May 2008, at 12:55, Cean O. wrote:

> fred pls help me as im really new to ruby… but need to rush some  
> project i cant really adopt ruby yet…

I can’t do your work for you 🙂 stick some puts statements in there  
so that you can see at each step whether variables have the expected  
values. Eg is page non-nil ?

Fred
