# Logstash substring a field using position getting error

**URL:** https://discuss.elastic.co/t/logstash-substring-a-field-using-position-getting-error/292157
**Category:** Logstash
**Created:** [December 16, 2021, 1:21pm UTC](https://discuss.elastic.co/t/logstash-substring-a-field-using-position-getting-error/292157 "2021-12-16T13:21:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![aeoker](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@aeoker](https://discuss.elastic.co/u/aeoker)
#### Post date: [December 16, 2021, 1:21pm UTC](https://discuss.elastic.co/t/logstash-substring-a-field-using-position-getting-error/292157/1 "2021-12-16T13:21:37Z")

</div>

Hello Everyone,  
I am getting below error while trying to produce new event from existing one as follows, any help?

```
    ruby { code => "event['new_field'] = event.get['dummy'][1..4]" }

```

Error:

> Ruby exception occurred: undefined method `[]' for nil:NilClass {:class=>"NoMethodError", :backtrace=>["(ruby filter code):2:in `block in filter\_method'", "./LOGSTASH/logstash-7.16.1/vendor/bundle/jruby/2.5.0/gems/logstash-filter-ruby-3.1.7/lib/logstash/filters/ruby.rb:93:in `inline_script'", "./LOGSTASH/logstash-7.16.1/vendor/bundle/jruby/2.5.0/gems/logstash-filter-ruby-3.1.7/lib/logstash/filters/ruby.rb:86:in `filter'", "./LOGSTASH/logstash-7.16.1/logstash-core/lib/logstash/filters/base.rb:159:in `do_filter'", "./LOGSTASH/logstash-7.16.1/logstash-core/lib/logstash/filters/base.rb:178:in `block in multi\_filter'", "org/jruby/RubyArray.java:1821:in `each'", "/evam/LOGSTASH/logstash-7.16.1/logstash-core/lib/logstash/filters/base.rb:175:in `multi\_filter'", "org/logstash/config/ir/compiler/AbstractFilterDelegatorExt.java:134:in `multi_filter'", "./LOGSTASH/logstash-7.16.1/logstash-core/lib/logstash/java_pipeline.rb:299:in `block in start\_workers'"]}

sample conf:

```auto
input {
        stdin { id => "inPut" }
}
filter{
        ruby { code => "event['new_field'] = event.get['dummy'][1..4]" }
}
output {
        stdout { codec => rubydebug }
}

```

---

<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: [December 16, 2021, 4:10pm UTC](https://discuss.elastic.co/t/logstash-substring-a-field-using-position-getting-error/292157/2 "2021-12-16T16:10:13Z")

</div>

> [@aeoker](#):
>
> undefined method `[]' for nil:NilClass`

That is telling you that the [dummy] field does not exist, so event.get returns nil, and an attempt to extract a substring of nil fails.

---

<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: [January 13, 2022, 4:10pm UTC](https://discuss.elastic.co/t/logstash-substring-a-field-using-position-getting-error/292157/3 "2022-01-13T16:10:32Z")

</div>

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