# Handling null in foreach with on\_failure

**URL:** <https://discuss.elastic.co/t/handling-null-in-foreach-with-on-failure/97629>\
**Category:** Elasticsearch\
**Created:** [August 18, 2017, 7:31pm UTC](https://discuss.elastic.co/t/handling-null-in-foreach-with-on-failure/97629 "2017-08-18T19:31:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [August 18, 2017, 7:31pm UTC](https://discuss.elastic.co/t/handling-null-in-foreach-with-on-failure/97629/1 "2017-08-18T19:31:43Z")

</div>

I have a pipeline where I `split` a field and then hand that off to a `foreach` processor. Sometimes the field is null. It appears that `foreach` throws an exception here regardless of whether I specify `on_failure`:

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "split": {
          "field": "foo",
          "separator": """(\s+)?,(\s+)?""",
          "ignore_missing": true
        }
      },
      {
        "foreach": {
          "field": "foo",
          "processor": {
            "uppercase": {
              "field": "_ingest._value",
              "on_failure": [
                {
                  "set": {
                    "field": "_ingest._value",
                    "value": "empty"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_type": "type",
      "_id": "id",
      "_source": {
        "foo": null
      }
    }
  ]
}

```

This gives the error:

````auto
  "docs": [
    {
      "error": {
        "root_cause": [
          {
            "type": "exception",
            "reason": "java.lang.IllegalArgumentException: java.lang.NullPointerException",
            "header": {
              "processor_type": "foreach"
            }
          }
        ],
        "type": "exception",
        "reason": "java.lang.IllegalArgumentException: java.lang.NullPointerException",
        "caused_by": {
          "type": "illegal_argument_exception",
          "reason": "java.lang.NullPointerException",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          }
        },
        "header": {
          "processor_type": "foreach"
        }
      }
    }
  ]
}```

If that is expected behavior, can someone suggest a workaround?
````

---

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [August 18, 2017, 7:49pm UTC](https://discuss.elastic.co/t/handling-null-in-foreach-with-on-failure/97629/2 "2017-08-18T19:49:08Z")

</div>

Ah, [ignore\_failure](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/handling-failure-in-pipelines.html#handling-failure-in-pipelines) works:

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "split": {
          "field": "foo",
          "separator": """(\s+)?,(\s+)?""",
          "ignore_missing": true
        }
      },
      {
        "foreach": {
          "field": "foo",
          "processor": {
            "uppercase": {
              "field": "_ingest._value"
            }
          },
          "ignore_failure": true
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_type": "type",
      "_id": "id",
      "_source": {
        "foo": null
      }
    }
  ]
}

```

---

<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:** [September 15, 2017, 7:49pm UTC](https://discuss.elastic.co/t/handling-null-in-foreach-with-on-failure/97629/3 "2017-09-15T19:49:32Z")

</div>

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