Null_pointer_exception when using aggregation

Hi Community,
I want to run this query

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

{
  "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"""

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

Try it:

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

Hi Alex,

The solution you provided is working for me , thank you :grinning:
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

{
        "_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

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

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

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