# Script painless : null\_pointer\_exception

**URL:** https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971
**Category:** Elasticsearch
**Created:** [March 28, 2018, 4:28pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971 "2018-03-28T16:28:13Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![sabdoul](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@sabdoul](https://discuss.elastic.co/u/sabdoul)
#### Post date: [March 28, 2018, 4:28pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/1 "2018-03-28T16:28:13Z")

</div>

Hello,

I have a null\_pointer\_exception error in my painless script when trying to modify all the data in an index. Indeed when I modify a line, the modification passes correctly but if I generalize it to all the data I have the error null\_pointer\_exception.

Below my code:

```
POST data-2018.02.20/_update_by_query
{
"query" : {
"match_all": {}
},
"script" : {
"source": "ctx._source.station.gps_geo_point.location = [ctx._source.station.gps.longitude,ctx._source.station.gps.latitude]",
"lang": "painless"
 }
}

```

And here is the error I get:

```
{
"error": {
  "root_cause": [
   {
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [],
    "script": "ctx._source['station.gps_geo_point.location'] = [ctx._source.station.gps.longitude,ctx._source.station.gps.latitude]",
    "lang": "painless"
  }
 ],
   "type": "script_exception",
   "reason": "runtime error",
  "script_stack": [],
  "script": "ctx._source['station.gps_geo_point.location'] = [ctx._source.station.gps.longitude,ctx._source.station.gps.latitude]",
  "lang": "painless",
"caused_by": {
   "type": "null_pointer_exception",
    "reason": null
   }
  },
  "status": 500
 }

```

PS: my version elastisearch is `5.6.8`

Thank you in advance for your help

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 28, 2018, 4:41pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/2 "2018-03-28T16:41:57Z")

</div>

You need to test if the field you want to access exists or not before using it IMO.

---

<div class="post-metadata">

### Author: ![sabdoul](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@sabdoul](https://discuss.elastic.co/u/sabdoul)
#### Post date: [March 28, 2018, 4:56pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/3 "2018-03-28T16:56:29Z")

</div>

Thank you for answering me,

Can you give me an example? because i don't know whether to do it with the query [exists](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-exists-query.html) or a condition in the painless script.

thank you!

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 28, 2018, 5:50pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/4 "2018-03-28T17:50:37Z")

</div>

I think you can try [https://www.elastic.co/guide/en/elasticsearch/painless/master/\_operators.html#\_null\_safe](https://www.elastic.co/guide/en/elasticsearch/painless/master/_operators.html#_null_safe)

(I never used yet). Otherwise a condition like `if ( ... ) { } else { }`...

---

<div class="post-metadata">

### Author: ![sabdoul](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@sabdoul](https://discuss.elastic.co/u/sabdoul)
#### Post date: [March 28, 2018, 5:58pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/5 "2018-03-28T17:58:40Z")

</div>

OK thanks,  
I'm going to test it and I'll confirm

Thank you for your help!

---

<div class="post-metadata">

### Author: ![sabdoul](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@sabdoul](https://discuss.elastic.co/u/sabdoul)
#### Post date: [March 29, 2018, 8:48am UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/6 "2018-03-29T08:48:16Z")

</div>

Hello dadoonet,

I tried with the operator null safe (?.) without success finally I opted with the condition if. But I still have a null\_pointer\_exception error.

Here is my code (note that I go a && to be sure that both fields are not null):

```
POST DATA-2018.02.20/_update_by_query
{
"query" : {
    "match_all": {}
},
"script" : {
    "source": "if(ctx._source.station.gps.longitude != null && ctx._source.station.gps.latitude != null) {ctx._source['station.gps_geo_point.location'] = [ctx._source.station.gps.longitude,ctx._source.station.gps.latitude]} else {ctx._source['station.gps_geo_point.location'] = [0,0]}",
    "lang": "painless"
 }
}

```

Error :

```
     {
 "error": {
"root_cause": [
  {
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [],
    "script": "if(ctx._source.station.gps.longitude != null && ctx._source.station.gps.latitude != null) {ctx._source['station.gps_geo_point.location'] = [ctx._source.station.gps.longitude,ctx._source.station.gps.latitude]} else {ctx._source['station.gps_geo_point.location'] = [0,0]}",
    "lang": "painless"
  }
],
"type": "script_exception",
"reason": "runtime error",
"script_stack": [],
"script": "if(ctx._source.station.gps.longitude != null && ctx._source.station.gps.latitude != null) {ctx._source['station.gps_geo_point.location'] = [ctx._source.station.gps.longitude,ctx._source.station.gps.latitude]} else {ctx._source['station.gps_geo_point.location'] = [0,0]}",
"lang": "painless",
"caused_by": {
  "type": "null_pointer_exception",
  "reason": null
}
},
"status": 500
}

```

I do not know why it does not work. The code seems correct to me to consider the null fields.

thank you in advance for your help!

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 29, 2018, 12:15pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/7 "2018-03-29T12:15:51Z")

</div>

I think you should check:

```
ctx._source.station.gps

```

And may be

```
ctx._source.station
```

---

<div class="post-metadata">

### Author: ![sabdoul](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@sabdoul](https://discuss.elastic.co/u/sabdoul)
#### Post date: [March 29, 2018, 1:11pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/8 "2018-03-29T13:11:26Z")

</div>

Thank you dadoonet,  
It works. it was `ctx._source.station`.

Thank you very much for your help 😁

---

<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: [April 26, 2018, 1:15pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/125971/9 "2018-04-26T13:15:36Z")

</div>

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