# Use ctx in if condition is always null\_pointer\_exception

**URL:** <https://discuss.elastic.co/t/use-ctx-in-if-condition-is-always-null-pointer-exception/317121>\
**Category:** Elasticsearch\
**Tags:** painless, ingest-pipeline\
**Created:** [October 20, 2022, 2:20pm UTC](https://discuss.elastic.co/t/use-ctx-in-if-condition-is-always-null-pointer-exception/317121 "2022-10-20T14:20:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HiganFish](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/higanfish/32/112335_2.png) [@HiganFish](https://discuss.elastic.co/u/HiganFish)\
**Post date:** [October 20, 2022, 2:20pm UTC](https://discuss.elastic.co/t/use-ctx-in-if-condition-is-always-null-pointer-exception/317121/1 "2022-10-20T14:20:49Z")

</div>

```auto
PUT _ingest/pipeline/test1
{
  "processors": [
    {"dissect": {
      "field": "message",
      "pattern": "%{logdata}"
    }}
  ]
}

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "dissect": {
          "field": "message",
          "pattern": "%{num1}"
        },
        "pipeline": {
          "name": "test1",
          "if": "ctx._source.num == '123'"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "message": "123"
      }
    }
  ]
}

```

I want to use the variable which generated by dissect or grok in if condition to use pipeline test1.

```auto
{
  "docs" : [
    {
      "error" : {
        "root_cause" : [
          {
            "type" : "script_exception",
            "reason" : "runtime error",
            "script_stack" : [
              "ctx._source.num == '123'",
              " ^---- HERE"
            ],
            "script" : "ctx._source.num == '123'",
            "lang" : "painless",
            "position" : {
              "offset" : 11,
              "start" : 0,
              "end" : 24
            }
          }
        ],
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "ctx._source.num == '123'",
          " ^---- HERE"
        ],
        "script" : "ctx._source.num == '123'",
        "lang" : "painless",
        "position" : {
          "offset" : 11,
          "start" : 0,
          "end" : 24
        },
        "caused_by" : {
          "type" : "null_pointer_exception",
          "reason" : null
        }
      }
    }
  ]
}

```

But it will give an error.

How should i use it.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 20, 2022, 2:53pm UTC](https://discuss.elastic.co/t/use-ctx-in-if-condition-is-always-null-pointer-exception/317121/2 "2022-10-20T14:53:15Z")

</div>

(edited) So a couple of items

There is no `ctx._source` in ingest pipelines

See: [here](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-ingest-processor-context.html)

that's the ssue, you should just remove `._source`

so perhaps try...

`"if": "ctx.num == '123'"`

Also just in case you are interesting in null safety

> **[Ingest pipelines | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html)**

> Incoming documents often contain object fields. If a processor script attempts to access a field whose parent object does not exist, Elasticsearch returns a `NullPointerException`. To avoid these exceptions, use [null safe operators](https://www.elastic.co/guide/en/elasticsearch/painless/8.4/painless-operators-reference.html#null-safe-operator), such as `?.`, and write your scripts to be null safe.
> 
> For example, `ctx.network?.name.equalsIgnoreCase('Guest')` is not null safe. `ctx.network?.name` can return null. Rewrite the script as `'Guest'.equalsIgnoreCase(ctx.network?.name)`, which is null safe because `Guest` is always non-null.
> 
> If you can’t rewrite a script to be null safe, include an explicit null check.

Also I did notice that above you name the field `num1` so that could also be a problem as well

` "pattern": "%{num1}"`

---

<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:** [November 17, 2022, 2:54pm UTC](https://discuss.elastic.co/t/use-ctx-in-if-condition-is-always-null-pointer-exception/317121/3 "2022-11-17T14:54:17Z")

</div>

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