# \_update\_by\_query throws null\_pointer\_exception

**URL:** https://discuss.elastic.co/t/update-by-query-throws-null-pointer-exception/248574
**Category:** Elasticsearch
**Created:** [September 14, 2020, 6:30pm UTC](https://discuss.elastic.co/t/update-by-query-throws-null-pointer-exception/248574 "2020-09-14T18:30:32Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Rui\_Goncalves](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rui_goncalves/32/72197_2.png) [@Rui\_Goncalves](https://discuss.elastic.co/u/Rui_Goncalves)
#### Post date: [September 14, 2020, 6:30pm UTC](https://discuss.elastic.co/t/update-by-query-throws-null-pointer-exception/248574/1 "2020-09-14T18:30:32Z")

</div>

I've created an index, and I'm trying to add one field. using this script:  
POST test\_kris\_assignment/\_update\_by\_query  
{  
"script": {  
"source": "ctx.\_source.field1.field2\_1 =10",  
"lang": "painless"  
},  
"query": {  
"match\_all": {}  
}  
}

field1 and field2\_1 fields don't exist, the idea it's to have field1 at parent levell and then values for fiels2\_1, field2\_2, field2\_3.... at same child level. Thank you. The complete error:

{  
"error": {  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"ctx.\_source.field1.field2\_1.index1 =10",  
" ^---- HERE"  
],  
"script": "ctx.\_source.field1.field2\_1.index1 =10",  
"lang": "painless"  
}  
],  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"ctx.\_source.field1.field2\_1.index1 =10",  
" ^---- HERE"  
],  
"script": "ctx.\_source.field1.field2\_1.index1 =10",  
"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: [September 17, 2020, 1:20pm UTC](https://discuss.elastic.co/t/update-by-query-throws-null-pointer-exception/248574/2 "2020-09-17T13:20:17Z")

</div>

please take your time to properly format your messages. This forums support markdown which in turn allows for super readable snippets.

You need to instantiate the `ctx._source.field1` first, before assign a variable inside of that map. Try `ctx._source.field1 = [:]` first and then your inside assignments.

---

<div class="post-metadata">

### Author: ![Rui\_Goncalves](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rui_goncalves/32/72197_2.png) [@Rui\_Goncalves](https://discuss.elastic.co/u/Rui_Goncalves)
#### Post date: [September 19, 2020, 12:52am UTC](https://discuss.elastic.co/t/update-by-query-throws-null-pointer-exception/248574/3 "2020-09-19T00:52:44Z")

</div>

Thank you so much, this was really helpful

---

<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: [October 17, 2020, 12:52am UTC](https://discuss.elastic.co/t/update-by-query-throws-null-pointer-exception/248574/4 "2020-10-17T00:52:49Z")

</div>

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