Script painless : null_pointer_exception

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

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

Thank you for answering me,

Can you give me an example? because i don't know whether to do it with the query exists or a condition in the painless script.

thank you!

I think you can try https://www.elastic.co/guide/en/elasticsearch/painless/master/_operators.html#_null_safe

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

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

Thank you for your help!

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!

I think you should check:

ctx._source.station.gps

And may be

ctx._source.station

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

Thank you very much for your help :grin: