# Runtime field script error: Cannot cast from \[java.lang.String\] to \[void\] in Kibana 8.17

**URL:** <https://discuss.elastic.co/t/runtime-field-script-error-cannot-cast-from-java-lang-string-to-void-in-kibana-8-17/380517>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [July 29, 2025, 3:24am UTC](https://discuss.elastic.co/t/runtime-field-script-error-cannot-cast-from-java-lang-string-to-void-in-kibana-8-17/380517 "2025-07-29T03:24:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hendrawns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrawns/32/144377_2.png) [@Hendrawns](https://discuss.elastic.co/u/Hendrawns)\
**Post date:** [July 29, 2025, 3:24am UTC](https://discuss.elastic.co/t/runtime-field-script-error-cannot-cast-from-java-lang-string-to-void-in-kibana-8-17/380517/1 "2025-07-29T03:24:00Z")

</div>

Hi everyone, I'm working with Kibana 8.17 and trying to create a runtime field using a Painless script to tag `http.request.referrer` into sectors based on domains.

I followed the docs here:

> **[Explore your data with runtime fields - Manage data views | Kibana Guide \[8.17\]...](https://www.elastic.co/guide/en/kibana/8.17/managing-data-views.html#runtime-fields)**

### Goal:

Create a runtime field named `referrer_sector`, of type `keyword`, that:

- Returns `"sector_a"` if the referrer contains `mywebsite.com`
- Returns `"other"` otherwise
- Returns `"unknown"` if the field is missing or empty

### Script I used:

```auto
if (doc.containsKey('http.request.referrer') && !doc['http.request.referrer'].empty) {
  String ref = doc['http.request.referrer'].value.toLowerCase();

  if (ref.contains("mywebsite.com")) {
    return "sector_a";
  } else {
    return "other";
  }
} else {
  return "unknown";
}

```

### Field Settings:

- Name: `referrer_sector`
- Type: `keyword`
- “Set value” is enabled

### Problem:

I keep getting the following error:

> Cannot cast from [java.lang.String] to [void]. Verify that you have correctly set the runtime field type.

Even though:

- I'm returning a string
- Type is explicitly set to `keyword`
- The field `http.request.referrer` exists and is visible in Discover

I even tried using `.keyword` subfield and checking `.size()` — same error.

Error Screenshot:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/8/a8f2400142cfcacdc59ec05aac91d3d788c19427.png)

- Is this a bug?
- Or is there something subtle I'm missing in setting up the script?

Any help is greatly appreciated!Thankyouu so much!

---

<div class="post-metadata">

**Author:** ![Tortoise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tortoise/32/147587_2.png) [@Tortoise](https://discuss.elastic.co/u/Tortoise)\
**Post date:** [July 29, 2025, 4:15am UTC](https://discuss.elastic.co/t/runtime-field-script-error-cannot-cast-from-java-lang-string-to-void-in-kibana-8-17/380517/2 "2025-07-29T04:15:28Z")

</div>

Hello @Hendrawns

I did check & see similar error message :

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/0/2042b05d173900fb5fb1da81a2d6e638ee5e46e0.png)

As per the documentation try using emit instead of return

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/2/0270023b050d7b429e4f3678806a3f498b671cbd.png)

Thanks!!

---

<div class="post-metadata">

**Author:** ![Hendrawns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrawns/32/144377_2.png) [@Hendrawns](https://discuss.elastic.co/u/Hendrawns)\
**Post date:** [July 30, 2025, 2:10am UTC](https://discuss.elastic.co/t/runtime-field-script-error-cannot-cast-from-java-lang-string-to-void-in-kibana-8-17/380517/3 "2025-07-30T02:10:15Z")

</div>

Ahhhh iseee, woah thankyouu so much for helping me 😆
