---
schema_version: "1"
id: 601a6fea72b4556e49c34480604eb136d368d457cdf818f13bd04a1a5de4bbb9
title: onNull
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.4/dw-core-functions-onnull.md
canonical_url: https://docs.mulesoft.com/dataweave/2.4/dw-core-functions-onnull
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 472ba9301865c53438e5a870bf06b3de44b73c2c47f63e63ec2bb5f10041a812
description: MuleSoft Documentation Site
release: "2.4"
latest: false
---
---
product: DataWeave
version: 2.4
---

# onNull

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## onNull<R>(previous: Null, callback: () -> R): R

Executes a callback function if the preceding expression returns a `null` value and then replaces
the `null` value with the result of the callback.

_Introduced in DataWeave version 2.4.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>previous</code></p></td><td><p>The value of the preceding expression.</p></td></tr><tr><td><p><code>callback</code></p></td><td><p>Callback that generates a new value if <code>previous</code> returns <code>null</code>.</p></td></tr></tbody></table>

### Example

This example shows how `onNull` behaves when it receives a `null` value.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{
     "onNull": []
             reduce ((item, accumulator) -> item ++ accumulator)
             then ((result) -> sizeOf(result))
             onNull "Empty Text"
 }
```

#### Output

```Json
{
  "onNull": "Empty Text"
}
```

## onNull<T>(previous: T, callback: () -> Any): T

Helper function that enables `onNull` to work with a _non-null_ value.

_Introduced in DataWeave version 2.4.0._
