Script painless: null pointer exception

I'm getting a null pointer exception when using null safe (?.) and after checking that the value is not null
CALL
POST mac_docs/mac_doc/_update_by_query?pretty=true&refresh=true
{
"script":{
"source":"if (params.mac_detail != null && params.mac_detail?.length > 0) {ctx._source.mac_details.add(params.mac_detail)}",
"lang": "painless",
"params": {
"mac_detail" : {

          }
        }
      },
      "query":{
        "term": {
          "mac_id": 44118
        }
      }
    }

ERROR

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "if(params.mac_detail != null && params.mac_detail?.length > 0) {",
          "                                                 ^---- HERE"
        ],
        "script": "if(params.mac_detail != null && params.mac_detail?.length > 0) {ctx._source.mac_details.add(params.mac_detail)}",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "if(params.mac_detail != null && params.mac_detail?.length > 0) {",
      "                                                 ^---- HERE"
    ],
    "script": "if(params.mac_detail != null && params.mac_detail?.length > 0) {ctx._source.mac_details.add(params.mac_detail)}",
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 500
}

I get the same error when using ?. on params.mac_detal
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"if(params.mac_detail != null && params?.mac_detail?.length > 0) {",
" ^---- HERE"
],
"script": "if(params.mac_detail != null && params?.mac_detail?.length > 0) {ctx._source.mac_details.add(params.mac_detail) }",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"if(params.mac_detail != null && params?.mac_detail?.length > 0) {",
" ^---- HERE"
],
"script": "if(params.mac_detail != null && params?.mac_detail?.length > 0) {ctx._source.mac_details.add(params.mac_detail) }",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
},
"status": 500
}

I even get the error when putting the length check inside the "successful" if check
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"if(params.mac_detail?.length > 0) { ",
" ^---- HERE"
],
"script": "if(params.mac_detail != null) { if(params.mac_detail?.length > 0) { ctx._source.mac_details.add(params.mac_detail) } }",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"if(params.mac_detail?.length > 0) { ",
" ^---- HERE"
],
"script": "if(params.mac_detail != null) { if(params.mac_detail?.length > 0) { ctx._source.mac_details.add(params.mac_detail) } }",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
},
"status": 500
}

Just realized I wasn't using [ ] on my list

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