# Having an issue while trying to reindex using a script

**URL:** <https://discuss.elastic.co/t/having-an-issue-while-trying-to-reindex-using-a-script/256915>\
**Category:** Elasticsearch\
**Created:** [November 27, 2020, 8:27pm UTC](https://discuss.elastic.co/t/having-an-issue-while-trying-to-reindex-using-a-script/256915 "2020-11-27T20:27:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tarek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tarek/32/142108_2.png) [@tarek](https://discuss.elastic.co/u/tarek)\
**Post date:** [November 27, 2020, 8:27pm UTC](https://discuss.elastic.co/t/having-an-issue-while-trying-to-reindex-using-a-script/256915/1 "2020-11-27T20:27:35Z")

</div>

Hello,

So, I'm trying to reindex documents into a new index with a stored script. the used script has a parameter called "increment". when I start reindexing, I get a null pointer exception.

This is my code.

```
POST _scripts/control_reindex_batch
{
  "script": {
    "lang": "painless",
    "source": """
      if (ctx._source.reindexBatch == null){
        ctx._source.reindexBatch += params.increment;
      }
      else{
        ctx._source.reindexBatch = 1;
      }
    """
  }
}

POST _reindex
{
  "source": {
    "index": "hamlet"
  },
  "dest": {
    "index": "hamlet-new"
  },
  "script": {
    "id": "control_reindex_batch",
    "params": {
      "increment": 1
    }
  }
}

```

I get the following error message:

```
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.reindexBatch += params.increment;\n }\n else{\n ",
          " ^---- HERE"
        ],
        "script": "control_reindex_batch",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.reindexBatch += params.increment;\n }\n else{\n ",
      " ^---- HERE"
    ],
    "script": "control_reindex_batch",
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![tarek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tarek/32/142108_2.png) [@tarek](https://discuss.elastic.co/u/tarek)\
**Post date:** [November 27, 2020, 8:29pm UTC](https://discuss.elastic.co/t/having-an-issue-while-trying-to-reindex-using-a-script/256915/2 "2020-11-27T20:29:38Z")

</div>

I forgot to mention that I'm using elasticsearch v7.2.1.

---

<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:** [December 25, 2020, 8:29pm UTC](https://discuss.elastic.co/t/having-an-issue-while-trying-to-reindex-using-a-script/256915/3 "2020-12-25T20:29:53Z")

</div>

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