# Null pointer exeption while creating a scripted metric

**URL:** <https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 24, 2020, 12:41pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646 "2020-02-24T12:41:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_Herrera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jorge_herrera/32/48640_2.png) [@Jorge\_Herrera](https://discuss.elastic.co/u/Jorge_Herrera)\
**Post date:** [February 24, 2020, 12:41pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/1 "2020-02-24T12:41:31Z")

</div>

Hi, I am creating a scripted metric to use it later for a transfrom. I am trying to get the count of the fields with a specific value, but i receive the next error:

```
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "state.errores.add(doc['TIPO_ERROR.keyword'].value=='Tecnológico' ? 1:0)",
          " ^---- HERE"
        ],
        "script": "state.errores.add(doc['TIPO_ERROR.keyword'].value=='Tecnológico' ? 1:0)",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "tandem_atm-0000001",
        "node": "KAgsKQnWQZGdUnUZN1itEA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "state.errores.add(doc['TIPO_ERROR.keyword'].value=='Tecnológico' ? 1:0)",
            " ^---- HERE"
          ],
          "script": "state.errores.add(doc['TIPO_ERROR.keyword'].value=='Tecnológico' ? 1:0)",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          }
        }
      }
    ]
  },
  "status": 400
}

```

Here is the code that i execute:

```
POST atm/_search
{
  "size": 0,
  "aggs":{
    "conteo":{
      "scripted_metric": {
        "init_script": "state.erorres = []",
        "map_script": "state.errores.add(doc['TIPO_ERROR.keyword'].value=='Tecnológico' ? 1:0)",
        "combine_script": "int conteo = 0; for (e in state.errores) { conteo += e } return conteo",
        "reduce_script": "int conteo = 0; for (a in states) { conteo += a } return conteo"
      }
    }
  }
}

```

I know i can use the filter aggregation to do that but my intention is to create a transform, and transforms dont accept filter aggregation.

Also i create a scripted field with the same script to make tests and it worked, so i dont know why in the scripted metric doesnt work.

Sorry for the gramaticals mistakes, Thank you

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 24, 2020, 1:01pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/2 "2020-02-24T13:01:18Z")

</div>

please do not paste screenshots they are nearly impossible to read. this forum supports markdown formatting, which is super useful for code snippets.

You need to check if that field exists before using it.

---

<div class="post-metadata">

**Author:** ![Jorge\_Herrera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jorge_herrera/32/48640_2.png) [@Jorge\_Herrera](https://discuss.elastic.co/u/Jorge_Herrera)\
**Post date:** [February 24, 2020, 2:32pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/3 "2020-02-24T14:32:10Z")

</div>

Thank you for your answer, as i said, i created an scripted field with the same script without problem, and i am sure that this field exist. Thats why i dont understand the problem.

P.S I change the screenshot, sorry for that

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 12, 2020, 10:24am UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/4 "2020-03-12T10:24:16Z")

</div>

try to recreate a minimal reproducible example. My assumption is, that a document from your result set does not have this field set.

---

<div class="post-metadata">

**Author:** ![Jorge\_Herrera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jorge_herrera/32/48640_2.png) [@Jorge\_Herrera](https://discuss.elastic.co/u/Jorge_Herrera)\
**Post date:** [March 17, 2020, 5:28pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/5 "2020-03-17T17:28:15Z")

</div>

And what can i do to identify if a document is missing that field ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 17, 2020, 5:34pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/6 "2020-03-17T17:34:39Z")

</div>

you can run a bool query, with a `must_not` clause, that contains an [exists query](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-exists-query.html) to find documents that do not contain this field.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Jorge\_Herrera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jorge_herrera/32/48640_2.png) [@Jorge\_Herrera](https://discuss.elastic.co/u/Jorge_Herrera)\
**Post date:** [March 17, 2020, 5:59pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/7 "2020-03-17T17:59:45Z")

</div>

Yes you were right, some of my documents fail in the grok parse that logstash. What i didn't know is that logstash actually index the documents that failed in the grok. I find documents with the tag "\_grokparsefailure" with your query. I guess i have to find a form to ignore the documents that failed in the grok, but that is a more logstash related question. Thank you Alexander for your help.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 18, 2020, 1:42pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/8 "2020-03-18T13:42:53Z")

</div>

Hey,

you can check with something like

```auto
if "_grokparsefailure" not in [tags] {
} else {
}

```

in the output to either ignore such events or write them to another index (the above example is on top of my head).

You could also check in your scripts if the field exists and check for `doc['TIPO_ERROR.keyword'].size() > 0` (also on top of my head)

--Alex

---

<div class="post-metadata">

**Author:** ![Jorge\_Herrera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jorge_herrera/32/48640_2.png) [@Jorge\_Herrera](https://discuss.elastic.co/u/Jorge_Herrera)\
**Post date:** [March 24, 2020, 12:36pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/9 "2020-03-24T12:36:03Z")

</div>

Hi, after doing some tests and do make changes i still get the error. Now i dont have documents without the field that i am trying to test. If i execute this query:

```
GET atm/_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "TIPO_ERROR"
                }
            }
        }
    }
}

```

This is the answer of elastic:

```
{
  "took" : 2070,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  }
}

```

So i asume all the documents have the field. So i execute this script:

```
POST atm/_search
{
  "aggs":{
    "conteo":{
      "scripted_metric": {
        "init_script": "state.erorres = []",
        "map_script": "if(doc['TIPO_ERROR.keyword'].size() > 0){state.errores.add(doc['TIPO_ERROR.keyword'].value =='Tecnológico' ? 1:0)}",
        "combine_script": "int conteo = 0; for (e in state.errores) { conteo += e } return conteo",
        "reduce_script": "int conteo = 0; for (a in states) { conteo += a } return conteo"
      }
    }
  }
}

```

But the response is still null\_pointer\_exception in the value, i really dont know what to do know, really aprecciate if you can help me. Thank you. I show you the response:

```
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "state.errores.add(doc['TIPO_ERROR.keyword'].value =='Tecnológico' ? 1:0)}",
          " ^---- HERE"
        ],
        "script": "if(doc['TIPO_ERROR.keyword'].size() > 0){state.errores.add(doc['TIPO_ERROR.keyword'].value =='Tecnológico' ? 1:0)}",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "tandem_atm-0000001",
        "node": "eo5tScCEQSSx_P3-TP3xMQ",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "state.errores.add(doc['TIPO_ERROR.keyword'].value =='Tecnológico' ? 1:0)}",
            " ^---- HERE"
          ],
          "script": "if(doc['TIPO_ERROR.keyword'].size() > 0){state.errores.add(doc['TIPO_ERROR.keyword'].value =='Tecnológico' ? 1:0)}",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          }
        }
      }
    ]
  },
  "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 24, 2020, 4:53pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/10 "2020-03-24T16:53:11Z")

</div>

Soooooooo 🙂

```auto
"init_script": "state.erorres = []",

```

```auto
"map_script": "... state.errores.add( ... )",

```

See the difference where the double-r is placed?

This worked for me for testing

```auto
        "init_script": "state.errores = []",
        "map_script": "if(doc['TIPO_ERROR.keyword'].value != null && doc['TIPO_ERROR.keyword'].value == 'foob') { state.errores.add(1) } else {state.errores.add(0)} ",

```

hope that helps.. in general, I'd try not to have a list, but maybe just two counters that are getting increased, so that you have less data being sent back and forth? Or even a single counter might be sufficient in this case... I guess it is simplifying

---

<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:** [April 21, 2020, 4:53pm UTC](https://discuss.elastic.co/t/null-pointer-exeption-while-creating-a-scripted-metric/220646/11 "2020-04-21T16:53:15Z")

</div>

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