# ElasticSearch Accessing Nested Documents in a Query Script - Null Pointer Exception

**URL:** <https://discuss.elastic.co/t/elasticsearch-accessing-nested-documents-in-a-query-script-null-pointer-exception/290980>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [December 5, 2021, 10:43am UTC](https://discuss.elastic.co/t/elasticsearch-accessing-nested-documents-in-a-query-script-null-pointer-exception/290980 "2021-12-05T10:43:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhirathmahipal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhirathmahipal/32/98119_2.png) [@abhirathmahipal](https://discuss.elastic.co/u/abhirathmahipal)\
**Post date:** [December 5, 2021, 10:43am UTC](https://discuss.elastic.co/t/elasticsearch-accessing-nested-documents-in-a-query-script-null-pointer-exception/290980/1 "2021-12-05T10:43:50Z")

</div>

Gist: Trying to write a custom filter on nested documents using painless. Want to write error checks when there are no nested documents to surpass `null_pointer_exception`

I have a mapping as such (simplified)

```auto
{
  "video_entry" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
       
        "captions_added" : {
          "type" : "boolean"
        },
        "category" : {
          "type" : "keyword"
        },
           
        "is_votable" : {
          "type" : "boolean"
        },
      
        "members" : {
          "type" : "nested",
          "properties" : {
            "country" : {
              "type" : "keyword",
            },
            "date_of_birth" : {
              "type" : "date",
            }
        }
   }
}

```

Each `video_entry` document can have 0 or more `members` nested documents.

Sample Document

```auto
{
   "captions_added": true,
   "category" : "Mental Health",
   "is_votable: : true,
   "members": [
        {"country": "Denmark", "date_of_birth": "1998-04-04T00:00:00"},
        {"country": "Denmark", "date_of_birth": "1999-05-05T00:00:00"}
   ]

}

```

If one or more nested document exist, we want to write some painless scripts that'd check certain fields across all the nested documents. My script works on mappings with a few documents but when I try it on larger set of documents I get null pointer exceptions despite having every null check possible. I've tried various access patterns, error checking mechanisms but I get exceptions.

```auto
POST /video_entry/_search
{
  "query": {
   "script": {
     "script": {
       "source": """
          // various NULL checks that I already tried
          // also tried short circuiting on finding null values
          if (!params['_source'].empty && params['_source'].containsKey('members')) {

              def total = 0;
          
          
              for (item in params._source.members) {
                // custom logic here
                // if above logic holds true 
                // total += 1; 
              } 
          
              return total > 3;
         }
         
         return true;
          
       """,
       "lang": "painless"
     }
   }
  }
}

```

Different tutorials use `ctx` , `doc` and some use `params` . To add to the confusion `Debug.explain(doc.members)` , `Debug.explain(params._source.members)` return empty responses and I'm having a hard time figuring out the types.

Any help is appreciated.

---

<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:** [January 2, 2022, 10:44am UTC](https://discuss.elastic.co/t/elasticsearch-accessing-nested-documents-in-a-query-script-null-pointer-exception/290980/2 "2022-01-02T10:44:38Z")

</div>

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