# Ruby exception occurred: undefined method \`\[\]' for nil:NilClass

**URL:** https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630
**Category:** Logstash
**Created:** [July 18, 2018, 10:28pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630 "2018-07-18T22:28:56Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Hanneu](https://avatars.discourse-cdn.com/v4/letter/h/7ab992/32.png) [@Hanneu](https://discuss.elastic.co/u/Hanneu)
#### Post date: [July 18, 2018, 10:28pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/1 "2018-07-18T22:28:57Z")

</div>

Hello experts,  
I have been getting very many below error and warning messages.

1.) [ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `[]' for nil:NilClass

2.) ][WARN][logstash.outputs.elasticsearch] Could not index event to Elasticsearch. {:status=\>400, :action=\>["index", {:\_id=\>nil, :\_index=\>"ind\_ecs\_airs\_counter-2018.07.18", :\_type=\>"logs", :\_routing=\>nil}, 2018-07-18T04:01:01.541Z POAIR001 DATE/TIME,EMAP Request V1,EMAP Response V1,EMAP Request V2,EMAP Response V2,ETSI MAP Request V2,ETSI MAP Response V2,ETSI MAP Request V3,ETSI MAP Response V3], :response=\>{"index"=\>{"\_index"=\>"ind\_ecs\_airs\_counter-2018.07.18", "\_type"=\>"logs", "\_id"=\>"AWSrjIbB6iFc8xSmLrcS", "status"=\>400, "error"=\>{"type"=\>"mapper\_parsing\_exception", "reason"=\>"failed to parse [ETSI\_MAP\_Response\_V3]", "caused\_by"=\>{"type"=\>"number\_format\_exception", "reason"=\>"For input string: "ETSI MAP Response V3""}}}}}

Can someone please suggest?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [July 18, 2018, 10:52pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/2 "2018-07-18T22:52:23Z")

</div>

For the first one, you need to show us at least the ruby filter from your logstash configuration.

For the second, you have a field that contains "ETSI MAP Response V3" and elasticsearch is expecting that field to be a number.

---

<div class="post-metadata">

### Author: ![Hanneu](https://avatars.discourse-cdn.com/v4/letter/h/7ab992/32.png) [@Hanneu](https://discuss.elastic.co/u/Hanneu)
#### Post date: [July 19, 2018, 4:56pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/3 "2018-07-19T16:56:52Z")

</div>

I am looking for ruby filter in files under logstash config (/etc/logstash/conf.d). However, I am not 100% sure how to check for ruby filter. Can you suggest?

Second issue, I understand elastic search is expecting the field "EPSI Map Response V3" to be in numeric format. However, is there a way we can change to string or any other non-numeric type?

---

<div class="post-metadata">

### Author: ![Hanneu](https://avatars.discourse-cdn.com/v4/letter/h/7ab992/32.png) [@Hanneu](https://discuss.elastic.co/u/Hanneu)
#### Post date: [July 19, 2018, 10:37pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/4 "2018-07-19T22:37:05Z")

</div>

For first issue, would the ruby filter be under /usr/share/logstash/logstash-core/lib/logstash/filters?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [July 19, 2018, 10:49pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/5 "2018-07-19T22:49:18Z")

</div>

No, I would expect it to be in your logstash config (/etc/logstash/conf.d or somewhere like that).

---

<div class="post-metadata">

### Author: ![Hanneu](https://avatars.discourse-cdn.com/v4/letter/h/7ab992/32.png) [@Hanneu](https://discuss.elastic.co/u/Hanneu)
#### Post date: [July 20, 2018, 3:30pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/6 "2018-07-20T15:30:26Z")

</div>

ruby  
{  
code =\> " event.set('TotalMap', event.get('EMAP\_Request\_V1').to\_i + event.get('EMAP\_Request\_V2').to\_i + event.get('ETSI\_MAP\_Request\_V2').to\_i + event.get('ETSI\_MAP\_Request\_V3').to\_i )  
event.set('SuccessMap', event.get('EMAP\_Response\_V1').to\_i + event.get('EMAP\_Response\_V2').to\_i + event.get('ETSI\_MAP\_Response\_V2').to\_i + event.get('ETSI\_MAP\_Response\_V3').to\_i ) "  
}

ruby  
{  
code =\> " if (event.get('BalanceEnquiryOut').to\_i + event.get('AccumulatorEnquiryOut').to\_i != 0)  
event.set('balance', ( event.get('BalanceEnquiryIn').to\_i + event.get('AccumulatorEnquiryIn').to\_i ) / ( event.get('BalanceEnquiryOut').to\_i + event.get('AccumulatorEnquiryOut').to\_i ))  
else  
event.set('balance', 0 )  
end "  
}

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [July 20, 2018, 3:43pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/7 "2018-07-20T15:43:39Z")

</div>

That's the kind of thing we are looking for. Although I would not expect those to produce that error. The NilClass could be produced by doing event.get on a field that does not exist. ruby seems to be OK doing to\_i on nil, so those filters should be OK. I'd be looking for something that indexes into an array without checking for nil first. A reference to something like

```
event.get("noSuchField")[0]
```

---

<div class="post-metadata">

### Author: ![Hanneu](https://avatars.discourse-cdn.com/v4/letter/h/7ab992/32.png) [@Hanneu](https://discuss.elastic.co/u/Hanneu)
#### Post date: [July 20, 2018, 3:51pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/8 "2018-07-20T15:51:45Z")

</div>

ruby  
{  
code =\> "  
event.to\_hash.keys.each { |k|  
if k.start\_with? '/Air:GetFaFList'  
event.set('GetFaFList',event.get('GetFaFList').to\_i + event.get(k).to\_i)

```
      elsif k.start_with? '/Air:UpdateFaFList'
        event.set('UpdateFaFList',event.get('UpdateFaFList').to_i + event.get(k).to_i)
```

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [July 20, 2018, 3:58pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/9 "2018-07-20T15:58:00Z")

</div>

k is certain to exist, so the start\_with? should be OK, and the only other thing that one does it to\_i, which we know is OK.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 17, 2018, 3:58pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/140630/10 "2018-08-17T15:58:17Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
