Reputation: 7280
I was using the following piece of code without any issues. the function querytags
returns a set of "products". geturl
function checks if a product has an image associated with it. All products with an image are pushed to productsProxy
array
@query = params[:tag]
@products = queryTags(@query)
@productsProxy = Array.new
if @products != nil
@products.each do |p|
tempProduct = ProductProxy.new(p)
if tempProduct.getUrl(tempProduct.images[0]['id'], 'small', tempProduct.images[0]['file'])
@productsProxy.push(tempProduct)
end
end
else
@productProxy = []
end
Then i tried to add another parameter in the URL and changed the querytags
function accordingly.
@query = params[:tag]
@taxon = params[:taxon]
@products = queryTags(@query, @taxon)
@productsProxy = Array.new
if @products != nil
@products.each do |p|
tempProduct = ProductProxy.new(p)
if tempProduct.getUrl(tempProduct.images[0]['id'], 'small', tempProduct.images[0]['file']) #now showing error
@productsProxy.push(tempProduct)
end
end
else
@productProxy = []
end
But I stated getting undefined method
[]' for nil:NilClass` on the line:
if tempProduct.getUrl(tempProduct.images[0]['id'], 'small', tempProduct.images[0]['file'])
I checked with the help of debugger that @products
array is not empty. I am unable to figure out why suddenly i am getting this error. please can someone help
Upvotes: 1
Views: 104
Reputation: 365
It's not about the "@products" array being empty or not - if it was empty, the "@products" iterator wouldn't do anything. The problem is that one of your products is either lacking an images attribute or the images[0] is not returning a hash. For debugging purposes I'd start by adding "break if p.images.nil?" to the top of your iterator and go from there.
Upvotes: 1