# Math Tags Null Value Error: Ending Position Out of Bounds (22) Stack Invalid Double

This error usually means a field inside a `<MATH>` tag is blank.

S-Docs expects a number.

If a merge field returns `null` instead, the calculation can fail with errors like:

* `Ending position out of bounds: 22 STACK`
* `Invalid double`

### Fix 1: Replace blank values with `0`

This is the fastest fix.

Add `replaceall=" ,0"` to any numeric field that might be blank.

{% code title="null-safe-math.xml" %}

```xml
<MATH>
  {{!Opportunity.Amount replaceall=" ,0"}} +
  {{!Opportunity.CustomField__c replaceall=" ,0"}}
</MATH>
```

{% endcode %}

Use this pattern when:

* the field is numeric
* blank values should behave like zero
* you want the calculation to always run

### Fix 2: Only run math when data exists

Use a named query and conditional logic when a missing value means the calculation should not run at all.

This pattern first checks whether matching records exist.

Then it runs the calculation only when there is data to use.

{% code title="conditional-math-with-named-queries.xml" %}

```xml
<!--{{!<LineItemsSOQL>
<class>none</class>
<queryname>myQuery1</queryname>
<soql>
SELECT count(ID) cid
FROM Opportunity
WHERE AccountId = '{{!Account.ID}}'
</soql>
</LineItemsSOQL>}}-->

<!--{{!<LineItemsSOQL>
<class>none</class>
<queryname>myQuery2</queryname>
<soql>
SELECT SUM(Amount) total, SUM(CustomField__c) reduce
FROM Opportunity
WHERE AccountId = '{{!Account.ID}}'
</soql>
</LineItemsSOQL>}}-->

<!--RENDER='{{!myQuery1.cid}} > 0' -->
<MATH>{{!myQuery2.total}} + {{!myQuery2.reduce}}</MATH>
<!--ENDRENDER-->
```

{% endcode %}

Use this pattern when:

* the calculation depends on query results
* zero would be misleading
* you only want output when records exist

{% hint style="info" %}
This issue is commonly tied to the `Invalid double` error. If you see both messages, check for blank numeric fields first.
{% endhint %}

### Quick checklist

Before you test again, confirm these items:

* every field inside `<MATH>` returns a number
* blank numeric fields use `replaceall=" ,0"` when needed
* query-based math is wrapped in `RENDER` when results may be missing
* your `<MATH>` tag opens and closes correctly

### Next step

For a beginner walkthrough, see [Get Started with Calculations](broken://spaces/WKNnJmhJBQwhdk5WBFsi/pages/AWClXpysUyYPXaqZa7po).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.sdocs.com/sdocs/advanced-template-logic/perform-calculations/math-tags-null-value-error-ending-position-out-of-bounds-22-stack-invalid-double.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
