title: undefined method text for NilClass
body: <p>I have a simple crawler written in Ruby that should crawl specific site and save data into a CSV file. When I am trying to run the script, I keep getting undefined method error :</p>
  
  <pre><code>boxers.rb:29:in `&lt;main&gt;': undefined method `text' for nil:NilClass (NoMethodError)
  </code></pre>
  
  <p>This is the code of script I'm trying to run:</p>
  
  <pre><code>#!/usr/bin/env ruby
  
  require 'csv'
  require 'mechanize'
  
  agent = Mechanize.new{ |agent| agent.history.max_size=0 }
  agent.user_agent = 'Mozilla/5.0'
  
  base = "http://siteurl.com/"
  
  division = ARGV[0]
  
  search_url = "http://siteurl.com/ratings.php?sex=M&amp;division=#{division}&amp;pageID="
  
  path='//*[@id="mainContent"]/table/tr[position()&gt;2]'
  
  boxers = CSV.open("csv/file.csv","w")
  
  url = search_url+"1"
  
  begin
    page = agent.get(url)
  rescue
    print "  -&gt; error, retrying\n"
    retry
  end
  
  // propably the line that causes error
  a = page.parser.xpath('//a[@title="last page"]').first.text
  a.gsub!("[","")
  a.gsub!("]","")
  
  last = a.to_i
  
  (1..last).each do |page|
  
    url = search_url+page.to_s
  
    begin
      page = agent.get(url)
    rescue
      print "  -&gt; error, retrying\n"
      retry
    end
  
    page.parser.xpath(path).each do |tr|
      row = [division]
      tr.xpath("td").each_with_index do |td,j|
        case j
        when 0,11
          next
        when 2
          text = td.text.strip
          a = td.xpath("a").first
          href = base+a.attributes["href"].value.strip
          human_id = href.split("=")[1].split("&amp;")[0]
          cat = href.split("=")[2]
          row += [human_id, cat, text, href]
        when 4
          text = td.text.strip
          record = text.split("-")
          wins = record[0]
          wko = wins.split("(")[1].split(")")[0] rescue 0
          wins = wins.split("(")[0]
          losses = record[1]
          lko = losses.split("(")[1].split(")")[0] rescue 0
          losses = losses.split("(")[0]
          draws = record[2]
          row += [wins, wko, losses, lko, draws, text]
        when 5
          last6 = []
          td.xpath("table/tr/td").each do |td2|
            outcome = td2.attributes["class"].value.strip rescue nil
            last6 += [outcome]
          end
          last6 = last6.to_s.gsub("[","{").gsub("]","}")
          row += [last6]
        when 9
          div = td.xpath("div").first
          flag = div.attributes["class"].value.strip rescue nil
          title = div.attributes["title"].value.strip rescue nil
          row += [flag,title]
        else
          text = td.text.strip
          row += [text]
        end
      end
      if (row.size&gt;2)
        boxers &lt;&lt; row
      end
    end
    boxers.flush
  
  end
  
  boxers.close
  </code></pre>