Undefined method `[]=' for nil:NilClass

Hi,

I am a beginner with ruby, i tried to use lightcsv but i have a problem
when i use a .csv file

ERROR

/file2.rb:114:in title=': undefined method[]=’ for nil:NilClass
(NoMethodError)
from file1.rb:19:in publish_post' from file1.rb:51 from /Library/Ruby/Gems/1.8/gems/lightcsv-0.2.2/lib/lightcsv.rb:142:ineach’
from /Library/Ruby/Gems/1.8/gems/lightcsv-0.2.2/lib/lightcsv.rb:51:in
foreach' from /Library/Ruby/Gems/1.8/gems/lightcsv-0.2.2/lib/lightcsv.rb:82:incall’
from /Library/Ruby/Gems/1.8/gems/lightcsv-0.2.2/lib/lightcsv.rb:82:in
open' from /Library/Ruby/Gems/1.8/gems/lightcsv-0.2.2/lib/lightcsv.rb:50:inforeach’
from file1.rb:48

files concerned:

file1.rb

LightCsv.foreach(config) do|row|
begin
post = MyClass.new(row[0], row[1], row[2])
[…]
end
end

file2.rb

if real.is_a? String
[…]
else
define_method("#{key}=") do |value|
@form[real] = value
end
end

if i replace row in file1.rb by row = [data1, data2, data3] it works

If someone can help me :smiley:

On Fri, Jan 13, 2012 at 13:17, thomas carlier [email protected]
wrote:

/file2.rb:114:in title=': undefined method []=’ for nil:NilClass
(NoMethodError)

That’s trying to tell you that at line 114 in file2.rb, you’re
assinging to a subscrip of something (i.e., trying to set an element
in it by using square brackets), but that something is nil. Probably
it’s somewhere where you think you have an array or a hash. If line
114 is where you do “@form[real] = value” then that probably means
that @form is nil. Take a look at the code that’s supposed to set
@form (and if that’s not a Rails controller, possibly also the code
that’s supposed to call that code). Then if you can’t understand why
it’s wrong, put it somewhere we can get to, post a link, and we’ll try
to help you figure it out.

-Dave

On Fri, Jan 13, 2012 at 2:17 PM, thomas carlier
[email protected] wrote:

from file1.rb:51
from file1.rb:48
[…]
define_method(“#{key}=”) do |value|
Posted via http://www.ruby-forum.com/.
Thomas,

The backtrace shows the line number where the exception was raised.
If you send the whole code attached would be easier to help you debug
it.
By the fragments you sent the only thing I can infer is…
you are trying to use the []= on a nil object.

Abinoam Jr.

@Dave

"If line 114 is where you do “@form[real] = value” then that probably
means that @form is nil. "

That’s the problem, when i use the .csv file @form=nil

if i replace the line

post = WordpressSite.new(row[0], row[1], row[2])

by

post = WordpressSite.new(“http://example.com”, “user”, “password”)

that’s why i don’t understand my mistake

Please find attached files i think the problem is in spam.rb

thanks for helping guys and sorry for my english

Thanks for helping Dave

The problem was not really in my code.
The problem appear when there’s a redirection on the website.
(.htaccess)

When it’s the case i lost my data and i’m not on the page i supposed to
be.
So it’s normal that when i seach for @form the result is nil

I added some code, to see i there’s a redirection on the webpage and it
works now

On Fri, Jan 13, 2012 at 21:18, thomas carlier [email protected]
wrote:

That’s the problem, when i use the .csv file @form=nil
if i replace the line
post = WordpressSite.new(row[0], row[1], row[2])
by
post = WordpressSite.new(“http://example.com”, “user”, “password”)
that’s why i don’t understand my mistake

I don’t understand when you’re having a problem. Please answer
these questions separately and simply, so that we can get a solid
handle on that.

  1. When is @form nil: when you use the row (as in the first example),
    or when you use literal values (as in the second example), or both?

  2. Assuming it’s not both, then does the code do what you want, in the
    other case?

  3. Have you looked at what you’re actually getting in “row”? How does
    it differ from what you pass as literal values? Does there seem to be
    any pattern to the difference? Can you post a CSV file (fake so you
    don’t reveal passwords), along with what you get from the CSV parser
    running on the same file?

-Dave