From: Mmcolli00 Mom Date: 2009-01-15T13:51:06+09:00 Subject: undefined method `[]' for nil:NilClass - how to prevent? This returns correct value so long as I do not use array[0][i]. The error is: >searchArray.rb:13: undefined method `[]' for nil:NilClass (NoMethodError) > from shifting1.rb:7:in `each_line' > from shifting1.rb:7 >Exit code: 1 > #************************************** #Searches through a text file and, at one row at a time, counts how many occurrences of the value exist for the column. For instance, if word, 'bird' exists in column1 row1 then count how many birds exist for the whole column. Next..count the occurrences of bird in the column 2. If the numbers do not match then outputs "Value does not contain a match:" +Value. I am having this issue with NilClass a lot this week. What do you think I should change? ##start code col1 = 0 col2 = 0 File.open('temp.txt', 'r+').each_line do |temp| array = [] i = 0 array = temp.chomp.split(",",0) arrayVal1 = array[0][i] arrayVal2 = array[1][i] if array[0] == arrayVal1 then col1 = col1 + 1 end if array[1] == arrayVal2 then col2 = col2 + 1 end i = i + 1 if col1 != col2 then puts "Value does not contain a match:" puts arrayVal1 end end ##end code Attachments: http://www.ruby-forum.com/attachment/3164/searchArray.rb -- Posted via http://www.ruby-forum.com/.