# Script painless: null pointer exception

**URL:** https://discuss.elastic.co/t/script-painless-null-pointer-exception/126109
**Category:** Elasticsearch
**Created:** [March 29, 2018, 2:56pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/126109 "2018-03-29T14:56:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Zsullivan](https://avatars.discourse-cdn.com/v4/letter/z/b487fb/32.png) [@Zsullivan](https://discuss.elastic.co/u/Zsullivan)
#### Post date: [March 29, 2018, 2:56pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/126109/1 "2018-03-29T14:56:06Z")

</div>

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  
}

---

<div class="post-metadata">

### Author: ![Zsullivan](https://avatars.discourse-cdn.com/v4/letter/z/b487fb/32.png) [@Zsullivan](https://discuss.elastic.co/u/Zsullivan)
#### Post date: [March 29, 2018, 7:42pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/126109/2 "2018-03-29T19:42:05Z")

</div>

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

---

<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, 7:42pm UTC](https://discuss.elastic.co/t/script-painless-null-pointer-exception/126109/3 "2018-04-26T19:42:25Z")

</div>

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