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

**URL:** https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-size-for-nil-nilclass/270409
**Category:** Logstash
**Created:** [April 16, 2021, 4:53pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-size-for-nil-nilclass/270409 "2021-04-16T16:53:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Eduard\_Abril](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eduard_abril/32/49885_2.png) [@Eduard\_Abril](https://discuss.elastic.co/u/Eduard_Abril)
#### Post date: [April 16, 2021, 4:53pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-size-for-nil-nilclass/270409/1 "2021-04-16T16:53:54Z")

</div>

Hi guys,

I have the next error on a ruby filter. I'd really appreciate any suggest

[2021-04-16T11:28:06,202][ERROR][logstash.filters.ruby][main-postilion-pipeline][87afa373e1d0191b4f869238d2908162c237ad31fe6074ec0a41fc36d14eb8fa] Ruby exception occurred: undefined method `size' for nil:NilClass

Here's my pipeline filter

```auto
> ruby {
> 
> code => "
> 
> event.set('msg1Lenght', event.get('msg-1').size)
> 
> event.set('msg2Lenght', event.get('msg-2').size)
> 
> "
> 
> }
> 
> 
> if [msg1Lenght] < 6 {
> 
> mutate {
> 
> update => { "msg-1" => "No se encontraron datos en el mensaje." }
> 
> }
> 
> }
> 
> if [msg2Lenght] < 6 {
> 
> mutate {
> 
> update => { "msg-2" => "No se encontraron datos de detalle en el mensaje." }
> 
> }
> 
> 
> }

```

---

<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: [April 16, 2021, 5:01pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-size-for-nil-nilclass/270409/2 "2021-04-16T17:01:51Z")

</div>

That suggests that one of those fields does not exist. Change

```
event.set('msg1Lenght', event.get('msg-1').size)

```

to

```
msg-1 = event.get('msg-1')
if msg-1
    event.set('msg1Lenght', msg-1.size)
else
    event.set('msg1Lenght', 0)
end

```

Likewise for msg-2

---

<div class="post-metadata">

### Author: ![Eduard\_Abril](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eduard_abril/32/49885_2.png) [@Eduard\_Abril](https://discuss.elastic.co/u/Eduard_Abril)
#### Post date: [April 19, 2021, 8:24pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-size-for-nil-nilclass/270409/3 "2021-04-19T20:24:00Z")

</div>

@Badger Thanks a lot, it worked perfectly.

---

<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: [May 17, 2021, 8:24pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-size-for-nil-nilclass/270409/4 "2021-05-17T20:24:56Z")

</div>

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