當我運行下面的代碼我收到此錯誤:未定義的方法'藝術家」的零:NilClass(NoMethodError)
#!/usr/bin/env ruby -rubygems
require File.join(File.dirname(__FILE__), 'authentication')
require "csv" # faster_csv (ruby 1.9)
lines = CSV.read(File.join(File.dirname(__FILE__), 'karaoke.csv')) # Exported an Excel file as CSV
lines.slice!(0) # remove header line
collection = StorageRoom::Collection.find('collection ID')
Song = collection.entry_class
lines.each do |row|
karaoke = Song.new(:artist => row[0], :song => row[1], :genre => row[2], :file => StorageRoom::File.new_with_filename("#{karaoke.artist}#{karaoke.song}.mov"))
if karaoke.save
puts "Misuero Karaoke Latino saved: #{karaoke.artist}, #{karaoke.song}, #{karaoke.genre}"
else
puts "Misuero Karaoke Latino could not be saved: #{karaoke.errors.join(', ')}"
end
end
和錯誤是:
import_csv.rb:15:in `block in <main>': undefined method `artist' for nil:NilClass (NoMethodError)
from import_csv.rb:14:in `each'
from import_csv.rb:14:in `<main>'
我感興趣瞭解爲何出現此錯誤以及解決方案。提前致謝!
它看起來像你的'卡拉OK'對象沒有實例化。 – 2012-04-04 12:20:59
@AdrienSchuler我不明白。它是不是在15行中實例化?歌曲在12行中被實例化了嗎?或者我錯過了什麼? – hanleyhansen 2012-04-04 12:25:17
您不能在您的卡拉OK對象構造函數中直接使用'StorageRoom :: File.new_with_filename(「#{karaoke.artist}#{karaoke.song} .mov」)'。當他尚未實例化時,您正嘗試訪問對象屬性。 – 2012-04-04 12:28:39