Logstash.filters.ruby ] Ruby exception occurred: undefined method `[]' for nil:NilClass

When I'm writing this code in logstash file(.conf file) and executing, It is giving the value but with exception.

filter {
ruby {
code => "
k= 0
while k <= 10 do
Test=JSON.parse(response.body)['hits']['hits'][k]['_source']['Test']
puts Test
k += 1
end
"
}
}

output;
4
logstash.filters.ruby ] Ruby exception occurred: undefined method `[]' for nil:NilClass

Please post the solution to the benefit of other people with this problem.