# Elasticsearch scripted\_metric null\_pointer\_exception

**URL:** <https://discuss.elastic.co/t/elasticsearch-scripted-metric-null-pointer-exception/209920>\
**Category:** Elasticsearch\
**Created:** [November 29, 2019, 3:22am UTC](https://discuss.elastic.co/t/elasticsearch-scripted-metric-null-pointer-exception/209920 "2019-11-29T03:22:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [November 29, 2019, 3:22am UTC](https://discuss.elastic.co/t/elasticsearch-scripted-metric-null-pointer-exception/209920/1 "2019-11-29T03:22:44Z")

</div>

I'm trying to use the `scripted_metric` aggs of Elasticsearch and normally, it's working perfectly fine with my other scripts

However, with script below, I'm encountering an error called "null\_pointer\_exception" but they're just copy-pasted scripts and working for 6 modules already

```auto
$max = 10;

{
    "query": {
        "match_all": {}
        //omitted some queries here, so I just turned it into match_all
    }
  },
  "aggs": {
    "ARTICLE_CNT_PDAY": {
      "histogram": {
        "field": "pub_date",
        "interval": "86400"
      },
      "aggs": {
        "LATEST": {
          "nested": {
            "path": "latest"
          },
          "aggs": {
            "SUM_SVALUE": {
              "scripted_metric": {
                "init_script": "
                    state.te = [];
                    state.g = 0;
                    state.d = 0;
                    state.a = 0;
                ",
                "map_script": "
                    if(state.d != doc['_id'].value){
                        state.d = doc['_id'].value;
                        state.te.add(state.a);
                        state.g = 0;
                        state.a = 0;
                    }
                    state.a = doc['latest.soc_mm_score'].value;
                ",
                "combine_script": "
                    state.te.add(state.a);
                    double count = 0;
                    for (t in state.te) {
                        count += ((t*10)/$max)
                    }
                    return count;
                ",
                "reduce_script": "
                    double count = 0;
                    for (a in states) {
                        count += a;
                    }
                    return count;
                "
              }
            }
          }
        }
      }
    }
  }
}

```

I tried running this script in Kibana, and here's the error message:

[![Error](https://us1.discourse-cdn.com/elastic/original/3X/3/2/32c5d4e2bdb686d11bc0c2b68d0b145756e51152.png)](https://i.stack.imgur.com/B9Ie1.png)

What I'm getting is, that there's something wrong with the `reduce_script` portion, tried to change this part:

**FROM**

```auto
for (a in states) {
    count += a;
}

```

**TO**

```auto
for (a in states) {
    count += 1;
}

```

And worked perfectly fine, I felt that the `a` variable isn't getting what it's supposed to hold

Any ideas here? Would appreciate your help, thank you very much!

---

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [November 29, 2019, 6:06am UTC](https://discuss.elastic.co/t/elasticsearch-scripted-metric-null-pointer-exception/209920/2 "2019-11-29T06:06:20Z")

</div>

The reason is explained [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html#_empty_buckets):

> If a parent bucket of the scripted metric aggregation does not collect any documents an empty aggregation response will be returned from the shard with a null value. In this case the reduce\_script's states variable will contain null as a response from that shard. reduce\_script's should therefore expect and deal with null responses from shards.

So obviously one of my buckets is empty, and need to deal with that null like this:

```auto
            "reduce_script": "
                double count = 0;                    
                for (a in states) {
                    count += (a ?: 0);
                }
                return count;
            "

```

---

<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:** [December 27, 2019, 6:14am UTC](https://discuss.elastic.co/t/elasticsearch-scripted-metric-null-pointer-exception/209920/3 "2019-12-27T06:14:30Z")

</div>

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