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

**URL:** https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/325411
**Category:** Logstash
**Created:** [February 13, 2023, 4:29pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/325411 "2023-02-13T16:29:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mc96](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@mc96](https://discuss.elastic.co/u/mc96)
#### Post date: [February 13, 2023, 4:29pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/325411/1 "2023-02-13T16:29:56Z")

</div>

I have a filter that is as follows:

```auto
	event.set('[json][event][packetloss1]', event.get('[packets-received]') / event.get('[packets-sent]') * 100)
	event.set('[json][event][packetlosspercentage]', 100 - event.get('[json][event][packetloss1]'))

```

What I am trying to do is calculate the % of packets that didn't make it ( % of Packets Lost) but no matter the figures input in Packets Sent / Received, packetloss1 is always equals to 0 and I'm unsure why that is

---

<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: [February 13, 2023, 6:49pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/325411/2 "2023-02-13T18:49:55Z")

</div>

I suspect [packets-received] and [packets-sent] are integers. Try

```
ruby {
    code => '
        received = event.get("[packets-received]")
        sent = event.get("[packets-sent]")
        if sent and received
            event.set("[json][event][packetlosspercentage]", 100 * (1.0 - received.to_f / sent.to_f))
        end
    '
}

```

---

<div class="post-metadata">

### Author: ![mc96](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@mc96](https://discuss.elastic.co/u/mc96)
#### Post date: [February 14, 2023, 9:25am UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/325411/3 "2023-02-14T09:25:03Z")

</div>

Thanks Badger,

I had some errors at first so had to tweak it a little bit but got this working:

```auto
event.set('[received]', event.get('[packets-received]'))
event.set('[sent]', event.get('[packets-sent]'))
event.set('[json][event][packetlosspercentage]', 100 * (1.0 - event.get('[received]').to_f / event.get('[sent]').to_f))

```

Only the output is still the same in the Logstash console -

```auto
[ERROR][logstash.filters.ruby][main] Ruby exception occurred: undefined method `*' for nil:NilClass

```

I can see in the event that comes into Elastic that received is 6000 and sent is 7265 but im having trouble getting this filter to perform those calculations - and yes it is an integer

---

<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: [March 14, 2023, 9:25am UTC](https://discuss.elastic.co/t/ruby-exception-occurred-undefined-method-for-nil-nilclass/325411/4 "2023-03-14T09:25:40Z")

</div>

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