# Bypass null\_pointer\_exception when sorting Scripted Fields Painless

**URL:** <https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [December 17, 2018, 2:12am UTC](https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059 "2018-12-17T02:12:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vkandiva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vkandiva/32/42845_2.png) [@vkandiva](https://discuss.elastic.co/u/vkandiva)\
**Post date:** [December 17, 2018, 2:12am UTC](https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059/1 "2018-12-17T02:12:53Z")

</div>

Hi All, I am using NEST to generate the following request to Elastic Search 6.3:

{  
"from": 0,  
"size": 14,  
"sort": [  
{  
"\_script": {  
"order": "asc",  
"type": "number"  
"script": {  
"source": "if( (doc['1'].value-doc['2'].value) \> 10 ) { (doc['1'].value-doc['2'].value) }"  
}  
}  
}  
]  
}

My script does return null which I cant control since it can be a dynamic script because of which I end up getting the following error:  
"error": {

- "root\_cause": [
  - {
    - "type": "null\_pointer\_exception",
    - "reason": null}],

- "type": "search\_phase\_execution\_exception",
- "reason": "all shards failed",
- "phase": "query",
- "grouped": true,...

How can I bypass this error? Is it possible to do this without modifying the script? The script can be dynamic and the query is generated through a NEST object. Why cant the script sort work with null value results?

---

<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:** [December 17, 2018, 8:05pm UTC](https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059/2 "2018-12-17T20:05:58Z")

</div>

I don't think the null pointer exception is because you are returning `null`, but instead because you are trying to access a field which has no values for a document. Are you sure _all_ documents value values for fields `1` and `2`? Could you please look in your elasticsearch log and find the full stack trace for the NullPointerException?

---

<div class="post-metadata">

**Author:** ![vkandiva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vkandiva/32/42845_2.png) [@vkandiva](https://discuss.elastic.co/u/vkandiva)\
**Post date:** [December 18, 2018, 12:23am UTC](https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059/3 "2018-12-18T00:23:03Z")

</div>

Well, If I change the script to:

if( (doc['1'].value-doc['2'].value) \> 10 ) { (doc['1'].value-doc['2'].value) } **else { return 0}**

The sort works fine. I dont think there seems to be an issue with the documents having nulls in them.

---

<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:** [December 18, 2018, 8:21pm UTC](https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059/4 "2018-12-18T20:21:20Z")

</div>

I think I see what is happening here now. When painless is missing a return statement, it will by default return `null`. By not having an `else` case for your `if`, the null was returned and causing an NPE when trying to convert to `double` (the return type of numeric sort scripts).

Is there a reason you can't keep the else case with 0 (or whatever else you want as the default when your condition is not met)?

---

<div class="post-metadata">

**Author:** ![vkandiva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vkandiva/32/42845_2.png) [@vkandiva](https://discuss.elastic.co/u/vkandiva)\
**Post date:** [December 18, 2018, 11:30pm UTC](https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059/5 "2018-12-18T23:30:04Z")

</div>

Well the script is mostly dynamic and could be anything since it is entered by a user according to the way he wants the data. There could be plenty of scripts like these in our system which we might not be able to correct. They run without any issues and display null in our grids. Is there a way to handle these nulls and handle them when sorting?

---

<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:** [December 18, 2018, 11:48pm UTC](https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059/6 "2018-12-18T23:48:43Z")

</div>

This is already changed in 6.5.0. The signature for numeric sort scripts changed from returning `Object` (which is what causes painless to do an implicit `return null`) to return `double`, which painless then will return `0.0` with a missing else. Any handling would have to be on the client side.

---

<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:** [January 15, 2019, 11:48pm UTC](https://discuss.elastic.co/t/bypass-null-pointer-exception-when-sorting-scripted-fields-painless/161059/7 "2019-01-15T23:48:44Z")

</div>

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