#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    nomethoderror "each" for nil class


    require 'rubygems'
    require 'nokogiri'
    require 'sqlite3'

    FIELD_NAMES = [['selectcity', 'VARCHAR'],['match', 'VARCHAR'], ['phone_no', 'NUMERIC'], ['name', 'VARCHAR'],['address', 'VARCHAR'] ]

    TABLE_DIV_ID = "#dgrSearch"
    OFILE = File.open('data-hold/tel-directory.txt', 'w')
    OFILE.puts( FIELD_NAMES.map{|f| f[0]}.join("\t") )

    DBNAME = "data-hold/tel-directory.sqlite"
    File.delete(DBNAME) if File.exists?DBNAME
    DB = SQLite3:atabase.new( DBNAME )

    TABLE_NAME = "telephone_records"
    DB_INSERT_STATEMENT = "INSERT into #{TABLE_NAME} values
    (#{FIELD_NAMES.map{'?'}.join(',')})"

    DB.execute "CREATE TABLE #{TABLE_NAME}(#{FIELD_NAMES.map{|f| "`#{f[0]}` #{f[1]}"}.join(', ')});"
    FIELD_NAMES.each do |fn|
    DB.execute "CREATE INDEX #{fn[2]} ON #{TABLE_NAME}(#{fn[0]})" unless fn[2].nil?
    end

    Dir.glob("data-hold/pages/*.html").reject{|f| f =~ /All match/}.each do |fname|
    meta_info = File.basename(fname, '.html').split('--')
    page = Nokogiri::HTML(open(fname))

    page.css("#{TABLE_DIV_ID} tr")[1..-2].each do |tr|
    data_tds = tr.css('td').map{ |td|
    td.text.gsub(/[$,](?=\d)/, '').gsub(/\302\240|\s/, ' ').strip
    }

    data_row = meta_info + data_tds
    OFILE.puts( data_row.join("\t"))
    DB.execute(DB_INSERT_STATEMENT, data_row)

    end
    end

    OFILE.close
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    Next time it would help if you'd tell us which line the error happens on (and use proper code tags). But from the looks of it there's only one line here where the expression you're calling each on might return nil:

    Code:
    page.css("#{TABLE_DIV_ID} tr")[1..-2].each do
    some_array[1..-2] will return nil if some_array is empty (yes, seriously), so 'page.css("#{TABLE_DIV_ID} tr")' apparently returns an empty array and that's why you get the error.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by sepp2k1
    Next time it would help if you'd tell us which line the error happens on (and use proper code tags). But from the looks of it there's only one line here where the expression you're calling each on might return nil:

    Code:
    page.css("#{TABLE_DIV_ID} tr")[1..-2].each do
    some_array[1..-2] will return nil if some_array is empty (yes, seriously), so 'page.css("#{TABLE_DIV_ID} tr")' apparently returns an empty array and that's why you get the error.
    could you tell me what to change over there please i am tired any idea .............i am newbie who just began learning ruby.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    Well that would depend. Is it expected that 'page.css("#{TABLE_DIV_ID} tr")' might return an empty array? If it isn't, you should investigate why it did in this case. If it is, you should check whether the array is empty before calling [1..-2] on it.

IMN logo majestic logo threadwatch logo seochat tools logo