# Painless script\_exception : ctx.\_source : null\_pointer\_exception

**URL:** https://discuss.elastic.co/t/painless-script-exception-ctx-source-null-pointer-exception/168885
**Category:** Elasticsearch
**Created:** [February 18, 2019, 6:17pm UTC](https://discuss.elastic.co/t/painless-script-exception-ctx-source-null-pointer-exception/168885 "2019-02-18T18:17:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![achile](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@achile](https://discuss.elastic.co/u/achile)
#### Post date: [February 18, 2019, 6:17pm UTC](https://discuss.elastic.co/t/painless-script-exception-ctx-source-null-pointer-exception/168885/1 "2019-02-18T18:17:22Z")

</div>

Hello,  
I am working with version elasticsearch 6.5.4, I added a new field to my existing index and I want to give it a value equal to the sum of some other fields. I used the following query in Kibana but it does not work, only the first field of the addition operation is recognized :

POST my\_index/\_update\_by\_query  
{  
"script": {  
"lang": "painless",  
"source": "ctx.\_source['num\_attractivity']=ctx.\_source['num\_comments']+ctx.\_source['num\_comments']"  
},  
"query": {  
"match\_all": {}  
}  
}

######Result :  
{  
"error": {  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"ctx.\_source['num\_attractivity']=ctx.\_source['num\_comments']+ctx.\_source['num\_comments']",  
" ^---- HERE"  
],  
"script": "ctx.\_source['num\_attractivity']=ctx.\_source['num\_comments']+ctx.\_source['num\_comments']",  
"lang": "painless"  
}  
],  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"ctx.\_source['num\_attractivity']=ctx.\_source['num\_comments']+ctx.\_source['num\_comments']",  
" ^---- HERE"  
],  
"ctx.\_source['num\_attractivity']=ctx.\_source['num\_comments']+ctx.\_source['num\_comments']",  
"lang": "painless",  
"caused\_by": {  
"type": "null\_pointer\_exception",  
"reason": null  
}  
},  
"status": 500  
}

#######

thank you.

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [February 18, 2019, 7:19pm UTC](https://discuss.elastic.co/t/painless-script-exception-ctx-source-null-pointer-exception/168885/2 "2019-02-18T19:19:35Z")

</div>

Are you certain that all of your documents have a non-null `num_comments` field?

---

<div class="post-metadata">

### Author: ![achile](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@achile](https://discuss.elastic.co/u/achile)
#### Post date: [February 18, 2019, 7:47pm UTC](https://discuss.elastic.co/t/painless-script-exception-ctx-source-null-pointer-exception/168885/3 "2019-02-18T19:47:45Z")

</div>

thank you for your reply;

The fields I would like to add are by default equal to 0 (type: long). If I change the order of the fields on the addition operation, the second field is pointed with the error.

I tested this script on other configuration and it works, now I do not know what is the origin of the problem.

just to correct:  
ctx.\_source['num\_attractivity']=ctx.\_source['num\_comments']+ctx.\_source['num\_reactions']

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [February 19, 2019, 4:33am UTC](https://discuss.elastic.co/t/painless-script-exception-ctx-source-null-pointer-exception/168885/4 "2019-02-19T04:33:07Z")

</div>

What I meant is that if any of those two fields is not present in any source document, then it is not 0 by default, it is null. So you either need to make sure the each document has both of those fields or to protect against null pointer exceptions in your code.

---

<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: [March 19, 2019, 4:33am UTC](https://discuss.elastic.co/t/painless-script-exception-ctx-source-null-pointer-exception/168885/5 "2019-03-19T04:33:09Z")

</div>

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