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;
          """
        }
      }
    }
  }
}