title: Ruby: undefined method `<<' for nil:NilClass
body: <p>I have a CSV that I like to save all my hash values on it. I am using nokogiri sax to parse a xml document and then save it to a CSV.</p>
  
  <p>It parse and saves the first xml file but when start parsing the second one, it stop and the error I get is this:</p>
  
  <p><strong>The error:</strong> <code>NoMethodError: undefined method</code>&lt;&lt;' for nil:NilClass`</p>
  
  <p><strong>the nil error is happing in the @infodata[:titles] &lt;&lt; @content</strong></p>
  
  <p><strong>The sax parser:</strong></p>
  
  <pre><code>require 'rubygems'
  require 'nokogiri'
  require 'csv'
  
  class MyDocument &lt; Nokogiri::XML::SAX::Document
  
    HEADERS = [ :titles, :identifier, :typeOfLevel, :typeOfResponsibleBody, 
                :type, :exact, :degree, :academic, :code, :text ]
  
    def initialize
       @infodata = {}
       @infodata[:titles] = Array.new([])
    end
  
    def start_element(name, attrs)
      @attrs = attrs
      @content = ''
    end
    def end_element(name)
      if name == 'title'
        Hash[@attrs]["xml:lang"]
        @infodata[:titles] &lt;&lt; @content
        @content = nil
      end
      if name == 'identifier'
         @infodata[:identifier] = @content
         @content = nil
      end
      if name == 'typeOfLevel'
         @infodata[:typeOfLevel] = @content
         @content = nil
      end
      if name == 'typeOfResponsibleBody'
         @infodata[:typeOfResponsibleBody] = @content
         @content = nil
      end
      if name == 'type'
         @infodata[:type] = @content
         @content = nil
      end
      if name == 'exact'     
         @infodata[:exact] = @content
         @content = nil
      end
      if name == 'degree'
         @infodata[:degree] = @content
         @content = nil
      end
      if name == 'academic'
         @infodata[:academic] = @content
         @content = nil
      end
      if name == 'code'
         Hash[@attrs]['source="vhs"']
         @infodata[:code] = @content 
         @content = nil
      end
      if name == 'ct:text'
         @infodata[:beskrivning] = @content
         @content = nil
      end 
    end
    def characters(string)
      @content &lt;&lt; string if @content
    end
    def cdata_block(string)
      characters(string)
    end
    def end_document
      File.open("infodata.csv", "ab") do |f|
        csv = CSV.generate_line(HEADERS.map {|h| @infodata[h] })
        csv &lt;&lt; "\n"
        f.write(csv)
      end
    end
  end
  </code></pre>
  
  <p><strong>creating new an object for every file that is store in a folder(47.000xml files):</strong></p>
  
  <pre><code>parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)
  counter = 0
  
  Dir.glob('/Users/macbookpro/Desktop/sax/info_xml/*.xml') do |item|
    parser.parse(File.open(item, 'rb'))
    counter += 1
    puts "Writing file nr: #{counter}"
  end
  </code></pre>
  
  <p><strong>3 xml files for trying the code:</strong> <a href="https://gist.github.com/2378898" rel="nofollow">https://gist.github.com/2378898</a> https://gist.github.com/2378901 <a href="https://gist.github.com/2378904" rel="nofollow">https://gist.github.com/2378904</a></p>