# Null\_pointer\_exception when using aggregation

**URL:** <https://discuss.elastic.co/t/null-pointer-exception-when-using-aggregation/370029>\
**Category:** Elasticsearch\
**Created:** [November 4, 2024, 5:12pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-using-aggregation/370029 "2024-11-04T17:12:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aniket\_Pant](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Aniket\_Pant](https://discuss.elastic.co/u/Aniket_Pant)\
**Post date:** [November 4, 2024, 5:12pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-using-aggregation/370029/1 "2024-11-04T17:12:11Z")

</div>

Hi Community,  
I want to run this query

```auto
GET employees/_search
{
  "aggs": {
    "Total Income": {
      "sum": {
        "field": "Package",
        "script": {
          "source": "if(params._source['name.keyword']=='Elan'){return params._source['Package']?.toString().length();}"
        }
      }
    }
  }
}

```

I am getting the below error

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "null_pointer_exception",
        "reason": """Cannot invoke "java.lang.Number.doubleValue()" because the return value of "org.elasticsearch.script.AggregationScript.execute()" is null"""
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "employees",
        "node": "WYRlvdsyTZSiqp3UTsTguQ",
        "reason": {
          "type": "null_pointer_exception",
          "reason": """Cannot invoke "java.lang.Number.doubleValue()" because the return value of "org.elasticsearch.script.AggregationScript.execute()" is null"""

```

---

<div class="post-metadata">

**Author:** ![Alex\_Salgado-Elastic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_salgado-elastic/32/103081_2.png) [@Alex\_Salgado-Elastic](https://discuss.elastic.co/u/Alex_Salgado-Elastic)\
**Post date:** [November 4, 2024, 6:57pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-using-aggregation/370029/2 "2024-11-04T18:57:22Z")

</div>

The issue is occurring because the script is returning `null` for documents where the condition is not met.

Try it:

```auto
GET employees/_search
{
  "aggs": {
    "Total Income": {
      "sum": {
        "script": {
          "source": """
            if (doc['name.keyword'].value == 'Elan') {
              return doc['Package'].value;
            }
            return 0;
          """
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Aniket\_Pant](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Aniket\_Pant](https://discuss.elastic.co/u/Aniket_Pant)\
**Post date:** [November 6, 2024, 5:41pm UTC](https://discuss.elastic.co/t/null-pointer-exception-when-using-aggregation/370029/3 "2024-11-06T17:41:18Z")

</div>

Hi Alex,

The solution you provided is working for me , thank you 😀  
I don't know why my condition is not working and i want to understand the difference between `params._source['field']`, `params._fields['field'].value` and `doc['field'].value`

In my current index i have this document which i have mentioned below

```auto
{
        "_index": "employees",
        "_id": "e42RJJIB1B-C7zDFRixi",
        "_score": 1,
        "_source": {
          "name": "Elan",
          "Designation": "Software Engineer",
          "Package": 800000,
          "Department": "IT",
          "City": "Bengaluru",
          "State": "Karnataka"
        }

```

Mapping for employee index

```auto
PUT employees
{
  "settings": {
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "name": {"type": "text","fields": {"keyword":{"type":"keyword"}}},
      "Designation": {"type": "text"},
      "Package": {"type": "long"},
      "Department":{"type": "text"},
      "City": {"type": "text"},
      "State": {"type": "text"}
    }
  }
}

```

Below aggregation is working which i got from you

```auto
GET employees/_search
{
  "aggs": {
    "Total Income": {
      "sum": {
        "field": "Package",
        "script": {
          "source": """
          if(doc['name.keyword'].value=='Elan'){
            return doc['Package'].value.toString().length();
          }
          return 0;
          """
        
      }
    }
  }
}
}

```

But mine is not working

```auto
GET employees/_search
{
  "aggs": {
    "Total Income": {
      "sum": {
        "field": "Package",
        "script": {
          "source":""" 
          if(params._source['name.keyword']=='Amit Sharma'){
            return params._source['Package'].value.toString().length();
          }
          return 0;
            """
        
      }
    }
  }
}
}

```

I would like to know

> i want to understand the difference between `params._source['field']`, `params._fields['field'].value` and `doc['field'].value`
