# Ctx.payload.hits NOT working error

**URL:** <https://discuss.elastic.co/t/ctx-payload-hits-not-working-error/280070>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-monitoring, painless\
**Created:** [July 30, 2021, 10:53am UTC](https://discuss.elastic.co/t/ctx-payload-hits-not-working-error/280070 "2021-07-30T10:53:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex14](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@alex14](https://discuss.elastic.co/u/alex14)\
**Post date:** [July 30, 2021, 10:53am UTC](https://discuss.elastic.co/t/ctx-payload-hits-not-working-error/280070/1 "2021-07-30T10:53:14Z")

</div>

hey there, I´m trying to create a watcher but i need to use some fields from my index to send that information via email but i always get the same error

"caused\_by": {  
"type": "null\_pointer\_exception",  
"reason": "Cannot invoke "Object.getClass()" because "callArgs[0]" is null"

This is my watcher

{  
"trigger": {  
"schedule": {  
"interval": "1m"  
}  
},  
"input": {  
"search": {  
"request": {  
"search\_type": "query\_then\_fetch",  
"indices": [  
".monitoring-es-\*"  
],  
"rest\_total\_hits\_as\_int": true,  
"body": {  
"size": 0,  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"timestamp": {  
"gte": "{{ctx.trigger.scheduled\_time}}||-5m",  
"lte": "{{ctx.trigger.scheduled\_time}}",  
"format": "strict\_date\_optional\_time||epoch\_millis"  
}  
}  
},  
{  
"exists": {  
"field": "node\_stats"  
}  
}  
]  
}  
},  
"aggs": {  
"metricAgg": {  
"max": {  
"field": "node\_stats.jvm.mem.heap\_used\_percent"  
}  
}  
}  
}  
}  
}  
},  
"condition": {  
"script": {  
"source": "if (ctx.payload.aggregations.metricAgg.value \> params.threshold) { return true; } return false;",  
"lang": "painless",  
"params": {  
"threshold": 69  
}  
}  
},  
"actions": {  
"email\_action": {  
"transform": {  
"script": {  
"source": "['items': ctx.payload.hits.collect(hit -\> ['HostName': hit.\_source.service.address, 'NodeName': hit.\_source.agent.hostname, 'Address': hit.\_source.source\_code.transport\_address, 'index': hit.\_index])]",  
"lang": "painless"  
}  
},  
"email": {  
"profile": "standard",  
"to": [  
"[ander.o@example.com](mailto:ander.o@example.com)"  
],  
"subject": "[{{ctx.metadata.name}}] node Heap: ({{ctx.payload.result}}%) \> 85%",  
"body": {  
"html": """

# Monitoring Cluster
  
The Alarm [{{ctx.metadata.name}}], detected excedeed 85% Heap usage of {{ctx.payload.result}} in the node {{NodeName}},  
  
  
  
  
  
  
  
  
  
  

```
            <tr>
              <td>{{NodeName}}</td>
              <td>{{HostName}}</td>
              <td>{{Address}}<td>
              <td>{{ctx.payload.aggregations.metricAgg.value}}<td>
            </tr>          

        </table>
        <BR>
        <BR>
        """
    }
  }
}

```

},  
"transform": {  
"script": {  
"source": "HashMap result = new HashMap(); result.result = ctx.payload.aggregations.metricAgg.value; return result;",  
"lang": "painless",  
"params": {  
"threshold": 69  
}  
}  
}  
}

this is part of the index

"hits" : [  
{  
"\_index" : ".monitoring",  
"\_type" : "\_doc",  
"\_id" : "IUHjJKJKJGvuyvytyiuhiuhuiho",  
"\_score" : 0.0,  
"\_source" : {  
"@timestamp" : "2020-06-30T06:44:22.897Z",  
"type" : "node\_stats",  
"event" : {  
"module" : "elasticsearch",  
},  
"service" : {  
"address" : "xxxxxx",  
"type" : "elasticsearch"  
},  
"agent" : {  
"name" : "XXXX",  
,  
"timestamp" : "2020-06-30T06:44:22.904Z",  
"source\_node" : {  
"transport\_address" : "192.128.1.0.0.0",  
"name" : "master"  
},  
"ecs" : {  
"version" : "1.7.0"  
},  
"id" : "3a81894984917881189",  
"name" : "xxxx",  
"hostname" : "xxxxx",  
},  
"node\_stats" : {  
"process" : {  
"cpu" : {  
"percent" : 0  
},  
"node\_master" : true,  
"jvm" : {  
"mem" : {  
"heap\_used\_in\_bytes" : 20273711512,  
"heap\_used\_percent" : 60,  
"heap\_max\_in\_bytes" : 33285996544  
}  
and the error is alway about

"caused\_by": {  
"type": "null\_pointer\_exception",  
"reason": "Cannot invoke "Object.getClass()" because "callArgs[0]" is null"  
}  
}  
},  
"reason": "Failed to transform payload"

here you have the error complete

{  
"watch\_id": "XXXXXXb",  
"node": "XXXXX",  
"state": "executed",  
"user": "elastic",  
"status": {  
"state": {  
"active": true,  
"timestamp": "2021-07-30T10:46:25.268Z"  
},  
"last\_checked": "2021-07-30T10:48:04.151Z",  
"last\_met\_condition": "2021-07-30T10:48:04.151Z",  
"actions": {  
"email\_action": {  
"ack": {  
"timestamp": "2020-07-30T10:46:25.268Z",  
"state": "awaits\_successful\_execution"  
},  
"last\_execution": {  
"timestamp": "2021-07-30T10:48:04.151Z",  
"successful": false,  
"reason": "Failed to transform payload"  
}  
}  
},  
"execution\_state": "executed",  
"version": -1  
},  
"trigger\_event": {  
"type": "schedule",  
"triggered\_time": "2021-07-30T10:48:04.151Z",  
"schedule": {  
"scheduled\_time": "2021-07-30T10:48:04.102Z"  
}  
},  
"input": {  
"search": {  
"request": {  
"search\_type": "query\_then\_fetch",  
"indices": [  
".monitoring-es-_"  
],  
"rest\_total\_hits\_as\_int": true,  
"body": {  
"size": 0,  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"timestamp": {  
"gte": "{{ctx.trigger.scheduled\_time}}||-5m",  
"lte": "{{ctx.trigger.scheduled\_time}}",  
"format": "strict\_date\_optional\_time||epoch\_millis"  
}  
}  
},  
{  
"exists": {  
"field": "node\_stats"  
}  
}  
]  
}  
},  
"aggs": {  
"metricAgg": {  
"max": {  
"field": "node\_stats.jvm.mem.heap\_used\_percent"  
}  
}  
}  
}  
}  
}  
},  
"condition": {  
"script": {  
"source": "if (ctx.payload.aggregations.metricAgg.value \> params.threshold) { return true; } return false;",  
"lang": "painless",  
"params": {  
"threshold": 69  
}  
}  
},  
"metadata": {  
"name": "monitoring-es-jvm-PERCENT",  
"xpack": {  
"type": "json"  
}  
},  
"result": {  
"execution\_time": "2021-07-30T10:48:04.151Z",  
"execution\_duration": 304,  
"input": {  
"type": "search",  
"status": "success",  
"payload": {  
"\_shards": {  
"total": 8,  
"failed": 0,  
"successful": 8,  
"skipped": 0  
},  
"hits": {  
"hits": [],  
"total": 275,  
"max\_score": null  
},  
"took": 302,  
"timed\_out": false,  
"aggregations": {  
"metricAgg": {  
"value": 71  
}  
}  
},  
"search": {  
"request": {  
"search\_type": "query\_then\_fetch",  
"indices": [  
".monitoring-es-_"  
],  
"rest\_total\_hits\_as\_int": true,  
"body": {  
"size": 0,  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"timestamp": {  
"gte": "2021-07-30T10:48:04.102Z||-5m",  
"lte": "2021-07-30T10:48:04.102Z",  
"format": "strict\_date\_optional\_time||epoch\_millis"  
}  
}  
},  
{  
"exists": {  
"field": "node\_stats"  
}  
}  
]  
}  
},  
"aggs": {  
"metricAgg": {  
"max": {  
"field": "node\_stats.jvm.mem.heap\_used\_percent"  
}  
}  
}  
}  
}  
}  
},  
"condition": {  
"type": "script",  
"status": "success",  
"met": true  
},  
"transform": {  
"type": "script",  
"status": "success",  
"payload": {  
"result": 71  
}  
},  
"actions": [  
{  
"id": "email\_action",  
"type": "email",  
"status": "failure",  
"transform": {  
"type": "script",  
"status": "failure",  
"reason": "runtime error",  
"error": {  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"hit -\> ['HostName': hit.\_source.service.address, 'NodeName': hit.\_source.agent.hostname, 'Address': hit.\_source.source\_code.transport\_address, 'index': hit.\_index])]",  
"^---- HERE"  
],  
"script": "['items': ctx.payload.hits.collect(hit -\> ['HostName': hit.\_source.service.address, 'NodeName': hit.\_source.agent.hostname, 'Address': hit.\_source.source\_code.transport\_address, 'index': hit.\_index])]",  
"lang": "painless",  
"position": {  
"offset": 35,  
"start": 35,  
"end": 200  
}  
}  
],  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"hit -\> ['HostName': hit.\_source.service.address, 'NodeName': hit.\_source.agent.hostname, 'Address': hit.\_source.source\_code.transport\_address, 'index': hit.\_index])]",  
"^---- HERE"  
],  
"script": "['items': ctx.payload.hits.collect(hit -\> ['HostName': hit.\_source.service.address, 'NodeName': hit.\_source.agent.hostname, 'Address': hit.\_source.source\_code.transport\_address, 'index': hit.\_index])]",  
"lang": "painless",  
"position": {  
"offset": 35,  
"start": 35,  
"end": 200  
},  
"caused\_by": {  
"type": "null\_pointer\_exception",  
"reason": "Cannot invoke "Object.getClass()" because "callArgs[0]" is null"  
}  
}  
},  
"reason": "Failed to transform payload"  
}  
]  
},  
"messages":   
}

| Node | Host Name | IP | % |
| --- | --- | --- | --- |

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 1, 2021, 10:16pm UTC](https://discuss.elastic.co/t/ctx-payload-hits-not-working-error/280070/2 "2021-08-01T22:16:51Z")

</div>

Welcome to our community! 😃

For future reference you can edit topics/posts and update formatting, it saves creating duplicate topics. Let's continue the discussion at [Ctx.payload.hits NOT working error (Good format)](https://discuss.elastic.co/t/ctx-payload-hits-not-working-error-good-format/280071)

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 1, 2021, 10:16pm UTC](https://discuss.elastic.co/t/ctx-payload-hits-not-working-error/280070/3 "2021-08-01T22:16:55Z")

</div>


