Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method [] for nil:NilClass (NoMethodError) in ruby... Why?

Here's my code:

begin
     items = CSV.read('somefile.csv')
     linesAmount = CSV.readlines('somefile.csv').size
     counter = 1
     while linesAmount >= counter do
       fullrow = items[counter]
       firstitem = fullrow[0] #This is the line that my code doesn't like.
       puts firstitem
       counter = counter + 1

     end


end

For some ruby doesn't like that one line, where I have firstitem = fullrow[0]. It throws me the undefined method [] error. BUT, in the console, I can still see it printing the firstitem... So it still prints it, but yet throws an error? What's going on?

However, if I take the first three lines in the while loop OUTSIDE of the while loop, and comment out everything in the loop other than the counter line, then I don't get any error. So if that firstitem line appears outside the while loop, the code thinks it's all fine.

Edit: I know that the arrays start from 0, but I specifically wanted counter to not care about the very first line. I forgot to mention that, sorry.

Edit2: Thank you everyone, I solved it by adding -1 after linesAmount, and it works!

like image 851
someone Avatar asked Dec 15 '25 07:12

someone


1 Answers

Looks like you have an off by one error, you are reading past the end of the items array. If there are 10 lines in the CSV file, the lines will be an array with indexes from 0 to 9, not 1 to 10.

Change your while to look like this

counter = 0
while counter < linesAmount
  ...
end

However a better approach overall would be to just do the following

CSV.readlines('somefile.csv').each do |line|
  puts line
end
like image 103
Marc Avatar answered Dec 16 '25 21:12

Marc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!