# Null Pointer exception when trying to reindex using script

**URL:** <https://discuss.elastic.co/t/null-pointer-exception-when-trying-to-reindex-using-script/149717>\
**Category:** Elasticsearch\
**Created:** [September 24, 2018, 5:56pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-trying-to-reindex-using-script/149717 "2018-09-24T17:56:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikesparr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikesparr/32/4619_2.png) [@mikesparr](https://discuss.elastic.co/u/mikesparr)\
**Post date:** [September 24, 2018, 5:56pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-trying-to-reindex-using-script/149717/1 "2018-09-24T17:56:43Z")

</div>

Dealing with a user-defined object in about 600k records and want to reindex to standardized field naming. Issue is field has space in name and I'm struggling to figure syntax in `script` to reference old field with space in name to move to field without.

Ideas/suggestions?

## Reindex attempt

```auto
{
    "source": {
        "index": "records-test"
    },
    "dest": {
        "index": "records-reindex-20180924"
    },
    "script": {
        "source": "ctx._source.subject.desired_location.point.lat = ctx._source.subject.list.custom_fields['Desired location'].value.location.point.lat; ctx._source.subject.desired_location.point.lon = ctx._source.subject.list.custom_fields['Desired location'].value.location.point.lon; ctx._source.subject.list.custom_fields.remove('Desired location')"
    }
}

```

## Yields the following error:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.subject.desired_location.point.lat = ctx._source.subject.list.custom_fields['Desired location'].value.location.point.lat; ",
          " ^---- HERE"
        ],
        "script": "ctx._source.subject.desired_location.point.lat = ctx._source.subject.list.custom_fields['Desired location'].value.location.point.lat; ctx._source.subject.desired_location.point.lon = ctx._source.subject.list.custom_fields['Desired location'].value.location.point.lon; ctx._source.subject.list.custom_fields.remove('Desired location')",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.subject.desired_location.point.lat = ctx._source.subject.list.custom_fields['Desired location'].value.location.point.lat; ",
      " ^---- HERE"
    ],
    "script": "ctx._source.subject.desired_location.point.lat = ctx._source.subject.list.custom_fields['Desired location'].value.location.point.lat; ctx._source.subject.desired_location.point.lon = ctx._source.subject.list.custom_fields['Desired location'].value.location.point.lon; ctx._source.subject.list.custom_fields.remove('Desired location')",
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 500
}

```

Updated with new attempt and related error

* * *

Even trying to simplify to copy point field to point field fails with NPE

```auto
POST /_reindex
{
    "source": {
        "index": "records-test"
    },
    "dest": {
        "index": "records-reindex-20180924"
    },
    "script": {
        "source": "ctx._source.subject.desired_location.point = ctx._source.subject.list.custom_fields['Desired location'].value.location.point; ctx._source.subject.list.custom_fields.remove('Desired location')"
    }
}

```

Yields:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.subject.desired_location.point = ctx._source.subject.list.custom_fields['Desired location'].value.location.point; ",
          " ^---- HERE"
        ],
        "script": "ctx._source.subject.desired_location.point = ctx._source.subject.list.custom_fields['Desired location'].value.location.point; ctx._source.subject.list.custom_fields.remove('Desired location')",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.subject.desired_location.point = ctx._source.subject.list.custom_fields['Desired location'].value.location.point; ",
      " ^---- HERE"
    ],
    "script": "ctx._source.subject.desired_location.point = ctx._source.subject.list.custom_fields['Desired location'].value.location.point; ctx._source.subject.list.custom_fields.remove('Desired location')",
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 500
}

```

---

<div class="post-metadata">

**Author:** ![mikesparr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikesparr/32/4619_2.png) [@mikesparr](https://discuss.elastic.co/u/mikesparr)\
**Post date:** [September 24, 2018, 8:02pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-trying-to-reindex-using-script/149717/4 "2018-09-24T20:02:53Z")

</div>

I've tried the painless syntax `new ()` as well thinking perhaps the field had to be initiated first.

- [https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-reference.html#new-instance-operator](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-reference.html#new-instance-operator)

Odd as I've reindex others that generate new fields without issue so unsure why the NPE here.

---

<div class="post-metadata">

**Author:** ![mikesparr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikesparr/32/4619_2.png) [@mikesparr](https://discuss.elastic.co/u/mikesparr)\
**Post date:** [September 24, 2018, 9:11pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-trying-to-reindex-using-script/149717/5 "2018-09-24T21:11:02Z")

</div>

It appears the Null Pointer Exception may be caused if the source field doesn't exist. I added conditional logic and a `noop` operation in the else block and the reindex worked.

Just a generic NPE is not helpful and the error should indicate that a field could not be found, or perhaps save a default value if null. This error message implied that that sink field had issues when in fact, the source field for some records was missing.

I think this is solved (at least the mystery NPE), but the error could be more useful.

### Working code

```auto
{
    "source": {
        "index": "records-test"
    },
    "dest": {
        "index": "records-reindex-20180924"
    },
    "script": {
        "source": "if( ctx._source.subject.list.custom_fields.containsKey(\"Desired location\") ){ ctx._source.subject.desired_location = ctx._source.subject.list.custom_fields['Desired location'].value } else if( ctx._source.subject.list.custom_fields.containsKey(\"Property location\") ) { ctx._source.subject.desired_location = ctx._source.subject.list.custom_fields['Property location'].value } else { ctx.op = \"noop\"; }"
    }
}

```

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [September 25, 2018, 11:25pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-trying-to-reindex-using-script/149717/6 "2018-09-25T23:25:53Z")

</div>

I appreciate the feedback here and agree error messages are often fairly obtuse in using scripts. Error message improvement is included in the long-term roadmap for Painless noted as one of the items in this issue ([https://github.com/elastic/elasticsearch/issues/31009](https://github.com/elastic/elasticsearch/issues/31009)).

---

<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:** [October 23, 2018, 11:26pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-trying-to-reindex-using-script/149717/7 "2018-10-23T23:26:00Z")

</div>

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