# Accessing nested array of objects in elastic search throws null pointer exception using painless script

**URL:** <https://discuss.elastic.co/t/accessing-nested-array-of-objects-in-elastic-search-throws-null-pointer-exception-using-painless-script/200599>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 22, 2019, 5:37pm UTC](https://discuss.elastic.co/t/accessing-nested-array-of-objects-in-elastic-search-throws-null-pointer-exception-using-painless-script/200599 "2019-09-22T17:37:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manish\_kakoti](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manish_kakoti/32/53285_2.png) [@manish\_kakoti](https://discuss.elastic.co/u/manish_kakoti)\
**Post date:** [September 22, 2019, 5:37pm UTC](https://discuss.elastic.co/t/accessing-nested-array-of-objects-in-elastic-search-throws-null-pointer-exception-using-painless-script/200599/1 "2019-09-22T17:37:26Z")

</div>

Here is the schema:

```auto
"mappings": {
    "_doc": {
      "properties": {
        "createdAt": {
          "type": "date"
        },
        "files": {
          "type": "nested",
          "properties": {
            "name": {"type": "text"},
            "contentType": {"type": "text"}
          }
        }
      }
    }
  }
}

```

if i try to run a painless script to iterate through the document:

```auto
{
   "script": {
      "inline": """
      int total = 0;
      for (int i = 0; i < ctx._source.files.length; ++i) {
            if (ctx._source.files[i].name== "1") {
            ctx._source.files.add(params.object)
          
          }
        """
   }
}
}

```

It throws me null pointer exception on "ctx.\_source.files"

Any idea why does it happen? and how to access nested array of objects in ES 6.7

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 25, 2019, 10:28am UTC](https://discuss.elastic.co/t/accessing-nested-array-of-objects-in-elastic-search-throws-null-pointer-exception-using-painless-script/200599/2 "2019-09-25T10:28:54Z")

</div>

You don't provide the context in which you want to use your script. I'm assuming you want to do an Update by Query or something like that?

One problem with your script is that you don't check whether every document actually contains a `files` object. I think the null pointer you're seeing is because of at least one document without any files.

Another problem with your script is that you're concurrently trying to change the `files` list while you're iterating over it. That will also lead to problems.

Here's how I would write the script:

```auto
"""
      if (ctx._source.files != null) {
        def list = [];
        
        for (file in ctx._source.files) {
          if ("1".equals(file.name)) {
            list.add(params.object)
          }
        }
        
        for (item in list) {
          ctx._source.files.add(item)
        }
      }
"""

```

---

<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, 2019, 10:28am UTC](https://discuss.elastic.co/t/accessing-nested-array-of-objects-in-elastic-search-throws-null-pointer-exception-using-painless-script/200599/3 "2019-10-23T10:28:54Z")

</div>

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