# Optional\<\> fields in new Java API

**URL:** <https://developer.squareup.com/forums/t/optional-fields-in-new-java-api/21801>\
**Category:** Questions\
**Tags:** java-sdk\
**Created:** [April 6, 2025, 11:52am UTC](https://developer.squareup.com/forums/t/optional-fields-in-new-java-api/21801 "2025-04-06T11:52:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![edward](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@edward](https://developer.squareup.com/forums/u/edward)\
**Post date:** [April 6, 2025, 11:52am UTC](https://developer.squareup.com/forums/t/optional-fields-in-new-java-api/21801/1 "2025-04-06T11:52:40Z")

</div>

The new Java SDK has practically all fields has Optional\<\> rather than the previous String, int, etc. This makes it much more cumbersome to work with - e.g. `location.name().orElse(null)`.  
Is there some feature in the SDK or in Java that makes this easier to deal with or must we do .get()/.orElse() on each field?

A real example:

```auto
RefundPaymentResponse resp = ...;
double refunded = resp.getRefund().get().getAmountMoney().getAmount().get()/100.0;

```

---

<div class="post-metadata">

**Author:** ![Square-Dev-AI](https://sea1.discourse-cdn.com/square/forums/user_avatar/developer.squareup.com/square-dev-ai/32/2513_2.png) [@Square-Dev-AI](https://developer.squareup.com/forums/u/Square-Dev-AI)\
**Post date:** [April 6, 2025, 11:53am UTC](https://developer.squareup.com/forums/t/optional-fields-in-new-java-api/21801/2 "2025-04-06T11:53:09Z")

</div>

👋 I’m Square Dev AI, here to help answer your question!  
Based on the information you’ve provided, the following documentation will help answer your question:

> **[Java SDK](https://developer.squareup.com/docs/sdks/java)**
>
> Use the Square Java library to build with Square APIs in a language-idiomatic way that reduces complexity without sacrificing API functionality.

> **[Using the Square Java SDK](https://developer.squareup.com/docs/sdks/java/using-java-sdk)**
>
> Introduction to using the Square Java SDK features.

> **Additional Documentation**
>
> [Retrieve Refunds](https://developer.squareup.com/docs/refunds-api/retrieve-refunds)  
> [Square Java SDK Quickstart](https://developer.squareup.com/docs/sdks/java/quick-start)  
> [Working with Monetary Amounts in Square APIs](https://developer.squareup.com/docs/build-basics/common-data-types/working-with-monetary-amounts)

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.  
Otherwise one of our Developer Advocates will provide additional assistance shortly.

---

<div class="post-metadata">

**Author:** ![Square-Dev-AI](https://sea1.discourse-cdn.com/square/forums/user_avatar/developer.squareup.com/square-dev-ai/32/2513_2.png) [@Square-Dev-AI](https://developer.squareup.com/forums/u/Square-Dev-AI)\
**Post date:** [April 7, 2025, 4:06pm UTC](https://developer.squareup.com/forums/t/optional-fields-in-new-java-api/21801/3 "2025-04-07T16:06:57Z")

</div>

The use of Optional\<\> in Java is a way to help prevent NullPointerExceptions by forcing you to think about the case where a value might not be present. It’s a form of making your code safer and more expressive.

In your example, you’re chaining multiple get() calls together. This could potentially lead to a NullPointerException if one of the intermediate steps returns an empty Optional.

To make this safer, you could use the map() function that’s available on Optional. This allows you to apply a function to the value inside the Optional, if it’s present. If the Optional is empty, map() just returns an empty Optional.

Here’s how you could rewrite your example:

```auto
RefundPaymentResponse resp = ...;
Optional<Double> refunded = resp.getRefund()
    .flatMap(refund -> refund.getAmountMoney())
    .flatMap(money -> money.getAmount())
    .map(amount -> amount / 100.0);

```

In this code, each flatMap() call is used to “unwrap” an Optional. If the Optional is empty, the rest of the chain is skipped and an empty Optional is returned. The final map() call converts the amount to a double, if it’s present.

This code will give you an Optional for the refunded amount. If any of the fields in the chain were missing, you’ll just get an empty Optional. You can then decide what to do with this Optional. For example, you could use orElse() to provide a default value if the Optional is empty:

```auto
double refundedAmount = refunded.orElse(0.0);

```

This approach makes it clear that the refunded amount might not always be present, and forces you to handle this case.

_This answer was reviewed by @Bryan-Square._

---

<div class="post-metadata">

**Author:** ![edward](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@edward](https://developer.squareup.com/forums/u/edward)\
**Post date:** [April 8, 2025, 5:01am UTC](https://developer.squareup.com/forums/t/optional-fields-in-new-java-api/21801/4 "2025-04-08T05:01:33Z")

</div>

Thanks for responding.

Can we assume that methods that return `Optional<>` will never return `null`?

i.e. `getAmountMoney() == null` will never be true; instead only need to check for `getAmountMoney().isPresent()` or `.isEmpty()` ?

---

<div class="post-metadata">

**Author:** ![Bryan-Square](https://sea1.discourse-cdn.com/square/forums/user_avatar/developer.squareup.com/bryan-square/32/38_2.png) [@Bryan-Square](https://developer.squareup.com/forums/u/Bryan-Square)\
**Post date:** [April 8, 2025, 4:02pm UTC](https://developer.squareup.com/forums/t/optional-fields-in-new-java-api/21801/5 "2025-04-08T16:02:52Z")

</div>

Yes, you can assume that methods returning `Optional<>` will not return `null`. Instead, they will return an `Optional` object, which may either be empty or contain a value. Therefore, you should use `isPresent()` or `isEmpty()` to check for the presence of a value, rather than checking for `null`.

This design choice helps prevent `NullPointerExceptions` and encourages you to think about how to handle cases where data might not be available. Using methods like `map()`, `flatMap()`, and `orElse()` allows you to work with these values more fluidly and safely. 🙂
