# Why do I get null pointer errors when sorting by a scripted field and increasing the size in a terms aggregation?

**URL:** <https://discuss.elastic.co/t/why-do-i-get-null-pointer-errors-when-sorting-by-a-scripted-field-and-increasing-the-size-in-a-terms-aggregation/97161>\
**Category:** Elasticsearch\
**Created:** [August 16, 2017, 1:07am UTC](https://discuss.elastic.co/t/why-do-i-get-null-pointer-errors-when-sorting-by-a-scripted-field-and-increasing-the-size-in-a-terms-aggregation/97161 "2017-08-16T01:07:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickian](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@patrickian](https://discuss.elastic.co/u/patrickian)\
**Post date:** [August 16, 2017, 1:07am UTC](https://discuss.elastic.co/t/why-do-i-get-null-pointer-errors-when-sorting-by-a-scripted-field-and-increasing-the-size-in-a-terms-aggregation/97161/1 "2017-08-16T01:07:25Z")

</div>

I got a weird error while sorting with a scripted field. I have something like

```
"agg_one": {
                  "terms": {
                    "field": "application.keyword",
                    "size": 100,
                    "order": {
                      "_count": "desc"
                    }
                  },
                  "aggs": {
                    "total_time": {
                      "sum": {
                        "script": {
                          "inline": "doc['action_end'] - doc['action_start']",
                          "lang": "expression"
                        }
                      }
                    },
                    "threads": {
                      "terms": {
                        "field": "thread.keyword",
                        "size": 100,
                        "order": {
                          "total_time": "desc"
                        }
                      },
                      "aggs": {
                        "total_time": {
                          "sum": {
                            "script": {
                              "inline": "doc['action_end'] - doc['action_start']",
                              "lang": "expression"
                            }
                          }
                        }
                      }
                    }
                  }
                }

```

Under the threads aggregation, it's sorted by total\_time, which is computed by script. If I do it like that, I get null pointer errors. Now, when I change it to sort by \_count: "desc", it works. What could the error there be?

Also, in another instance, If i change the size from 100 to 1000 in the threads aggregate, It also throws null pointer errors. Again, why could that be? I have aggregates that have less than 100 but it still works fine when the size is set to 100. Advanced thanks to anyone who could shed some light as to why I'm getting these errors. Thank you.

Elasticsearch version is 5.5.0 by the way.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 18, 2017, 6:22am UTC](https://discuss.elastic.co/t/why-do-i-get-null-pointer-errors-when-sorting-by-a-scripted-field-and-increasing-the-size-in-a-terms-aggregation/97161/2 "2017-08-18T06:22:03Z")

</div>

Do you see a stack trace for the null pointer in your logs? If not, can you run your query with `error_trace=true` (see [https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#common-options-error-options](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#common-options-error-options)).

---

<div class="post-metadata">

**Author:** ![patrickian](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@patrickian](https://discuss.elastic.co/u/patrickian)\
**Post date:** [August 25, 2017, 4:03am UTC](https://discuss.elastic.co/t/why-do-i-get-null-pointer-errors-when-sorting-by-a-scripted-field-and-increasing-the-size-in-a-terms-aggregation/97161/3 "2017-08-25T04:03:11Z")

</div>

I added "missing: 0" in the total\_time aggregations and the error went away. I guess there were records not having the total\_time aggregation?

---

<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:** [September 22, 2017, 4:03am UTC](https://discuss.elastic.co/t/why-do-i-get-null-pointer-errors-when-sorting-by-a-scripted-field-and-increasing-the-size-in-a-terms-aggregation/97161/4 "2017-09-22T04:03:13Z")

</div>

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