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

**URL:** https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/86437
**Category:** Logstash
**Created:** [May 19, 2017, 1:56pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/86437 "2017-05-19T13:56:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Pierre\_Vincent\_Ledou](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pierre_vincent_ledou/32/1368_2.png) [@Pierre\_Vincent\_Ledou](https://discuss.elastic.co/u/Pierre_Vincent_Ledou)
#### Post date: [May 19, 2017, 1:56pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/86437/1 "2017-05-19T13:56:36Z")

</div>

I have the following ruby filter:

```
mutate {
  convert => ["body_size", "integer"]
  convert => ["header_size", "integer"]
}
# Sum of body and header size
ruby {
  code => "event.set('bytes', (event.get('body_size') + event.get('header_size')))"
}

```

Where body\_size or header\_size can be 0, but in those case, I get the following error:

`Ruby exception occurred: undefined method`+' for nil:NilClass`

How can I avoid that error and ensure that I get the sum of the field, even if there are 0?

---

<div class="post-metadata">

### Author: ![Nico-DF](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@Nico-DF](https://discuss.elastic.co/u/Nico-DF)
#### Post date: [May 19, 2017, 2:18pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/86437/2 "2017-05-19T14:18:18Z")

</div>

The problem isn't that there is 0 values, it's that some document that go through this filter doesn't have the 'body\_size' or header\_size field.

Either filter before to be sure that this field exists:

```auto
if [body_size]{
...
}

```

Or you do it in ruby and all doc that go through this will have the field bytes. For ruby, you can do this:

```auto
code => "
  sum = ( (event.get('body_size') != nil)?event.get('body_size') :0)+ ( (event.get('header_size') != nil)?event.get('body_size') :0)
  event.set('bytes', sum)
"

```

---

<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: [June 16, 2017, 2:18pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/86437/3 "2017-06-16T14:18:24Z")

</div>

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