# Painless scripted field can't use ctx.\_source

**URL:** <https://discuss.elastic.co/t/painless-scripted-field-cant-use-ctx-source/171138>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [March 6, 2019, 2:39pm UTC](https://discuss.elastic.co/t/painless-scripted-field-cant-use-ctx-source/171138 "2019-03-06T14:39:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Woo\_Lee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_woo_lee/32/14495_2.png) [@John\_Woo\_Lee](https://discuss.elastic.co/u/John_Woo_Lee)\
**Post date:** [March 6, 2019, 2:39pm UTC](https://discuss.elastic.co/t/painless-scripted-field-cant-use-ctx-source/171138/1 "2019-03-06T14:39:20Z")

</div>

Hello

I'm using using 6.4.2

I am trying to use the `ctx._source` syntax to create a custom field but it just gives error. Using a basic example, all documents have a `hostname` field of type string. I've tried many permutations of trying to assign `ctx._source.hostname` to the customer field but nothing works.

```
"reason": {
					"type": "script_exception",
					"reason": "runtime error",
					"script_stack": ["if (ctx._source.hostname != null) { ", " ^---- HERE"],
					"script": "if (ctx._source.hostname != null) { ctx._source.hostname; }",
					"lang": "painless",
					"caused_by": {
						"type": "null_pointer_exception",
						"reason": null
					}
				}

```

Is there a better way to debug this, or can someone suggest a fix please ?

thanks

---

<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 3, 2019, 2:39pm UTC](https://discuss.elastic.co/t/painless-scripted-field-cant-use-ctx-source/171138/2 "2019-04-03T14:39:20Z")

</div>

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