# Painless script error

**URL:** <https://discuss.elastic.co/t/painless-script-error/260709>\
**Category:** Kibana\
**Created:** [January 11, 2021, 11:51am UTC](https://discuss.elastic.co/t/painless-script-error/260709 "2021-01-11T11:51:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_Amundsen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andreas_amundsen/32/78646_2.png) [@Andreas\_Amundsen](https://discuss.elastic.co/u/Andreas_Amundsen)\
**Post date:** [January 11, 2021, 11:51am UTC](https://discuss.elastic.co/t/painless-script-error/260709/1 "2021-01-11T11:51:00Z")

</div>

Hi, I'm trying to create a scripted field with the simple painless-code bellow.  
`Math.round((doc['CRC_OK'].value / doc['NR_UWs'].value)*100)/100.0`  
Everything seems to be working when I preview the code:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/a/4aedaa6e49e1f3a1006e0f8e84d3aefd37de35dd.png)  
When I open the discover-page I immediately get an error like this:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/0/70a021bbce7d818a01b6b9cab7ba3b3ee58ed5d6.png)  
I tried to remove the multiplication and divisiion of 100, but that didn't make any difference.  
I would be grateful if you could help my find the bug.

Thanks!

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [January 11, 2021, 12:31pm UTC](https://discuss.elastic.co/t/painless-script-error/260709/2 "2021-01-11T12:31:57Z")

</div>

Is it possible there are documents in your indices which don't have a value for either `CRC_OK` or `NR_UWs`?

A typical solution is to safeguard the script with checks for all used fields:

```auto
if (doc['CRC_OK'].size()==0 || doc['NR_UWs'].size()==0) return;
return Math.round((doc['CRC_OK'].value / doc['NR_UWs'].value)*100)/100.0

```

---

<div class="post-metadata">

**Author:** ![Andreas\_Amundsen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andreas_amundsen/32/78646_2.png) [@Andreas\_Amundsen](https://discuss.elastic.co/u/Andreas_Amundsen)\
**Post date:** [January 11, 2021, 12:45pm UTC](https://discuss.elastic.co/t/painless-script-error/260709/3 "2021-01-11T12:45:54Z")

</div>

You were right that one document was missing the values. Your code threw an error as well, but after a small change it worked!

```auto
if (doc['CRC_OK'].size()>0 || doc['NR_UWs'].size()>0) {
return Math.round((doc['CRC_OK'].value / doc['NR_UWs'].value)*100)/100.0
}

```

Thanks a lot!

---

<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:** [February 8, 2021, 12:46pm UTC](https://discuss.elastic.co/t/painless-script-error/260709/4 "2021-02-08T12:46:30Z")

</div>

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