René's URL Explorer Experiment
Title: Objects.requireNonNullElse() | java.evolved
Open Graph Title: Objects.requireNonNullElse() | java.evolved
X Title: Objects.requireNonNullElse() | java.evolved
Description: Get a non-null value with a clear default, no ternary needed.
Open Graph Description: Get a non-null value with a clear default, no ternary needed.
X Description: Get a non-null value with a clear default, no ternary needed.
Opengraph URL: https://javaevolved.github.io/errors/require-nonnull-else.html
direct link
Domain: javaevolved.github.io
Hey, it has json ld scripts:
{
"@context": "https://schema.org",
"@type": "TechArticle",
"headline": "Objects.requireNonNullElse()",
"description": "Get a non-null value with a clear default, no ternary needed.",
"url": "https://javaevolved.github.io/require-nonnull-else.html",
"publisher": {
"@type": "Organization",
"name": "java.evolved",
"url": "https://javaevolved.github.io"
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://javaevolved.github.io/require-nonnull-else.html"
}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": 1,
"name": "Home",
"item": "https://javaevolved.github.io/"
},
{
"@type": "ListItem",
"position": 2,
"name": "Errors",
"item": "https://javaevolved.github.io/#errors"
},
{
"@type": "ListItem",
"position": 3,
"name": "Objects.requireNonNullElse()"
}
]
}
| theme-color | #f97316 |
| mobile-web-app-capable | yes |
| apple-mobile-web-app-status-bar-style | black-translucent |
| apple-mobile-web-app-title | java.evolved |
| og:type | article |
| og:site_name | java.evolved |
| og:locale | en |
| og:image | https://javaevolved.github.io/og/errors/require-nonnull-else.png |
| og:image:width | 1200 |
| og:image:height | 630 |
| og:image:type | image/png |
| twitter:card | summary_large_image |
| twitter:image | https://javaevolved.github.io/og/errors/require-nonnull-else.png |
Links:
| java.evolved | https://javaevolved.github.io/ |
|
| https://github.com/javaevolved/javaevolved.github.io |
| ← All patterns | https://javaevolved.github.io/ |
| ← | https://javaevolved.github.io/errors/optional-chaining.html |
| → | https://javaevolved.github.io/errors/multi-catch.html |
| Home | https://javaevolved.github.io/ |
| Errors | https://javaevolved.github.io/#errors |
| 🐛 Report a code issue | https://github.com/javaevolved/javaevolved.github.io/issues/new?template=code-issue.yml&title=%5BCode%20Issue%5D%20Objects.requireNonNullElse%28%29&category=errors&slug=require-nonnull-else |
| 🌐 Report a translation issue | https://github.com/javaevolved/javaevolved.github.io/issues/new?template=translation-issue.yml&title=%5BTranslation%5D%20Objects.requireNonNullElse%28%29%20%28English%29&locale=en&pattern=require-nonnull-else&area=Pattern%20content |
| 💡 Suggest a new pattern | https://github.com/javaevolved/javaevolved.github.io/issues/new?template=new-pattern.yml |
| 𝕏 | https://x.com/intent/tweet?url=https%3A%2F%2Fjavaevolved.github.io%2Ferrors%2Frequire-nonnull-else.html&text=Objects.requireNonNullElse%28%29%20%E2%80%93%20java.evolved |
| 🦋 | https://bsky.app/intent/compose?text=Objects.requireNonNullElse%28%29%20%E2%80%93%20java.evolved%20https%3A%2F%2Fjavaevolved.github.io%2Ferrors%2Frequire-nonnull-else.html |
| in | https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fjavaevolved.github.io%2Ferrors%2Frequire-nonnull-else.html |
| ⬡ | https://www.reddit.com/submit?url=https%3A%2F%2Fjavaevolved.github.io%2Ferrors%2Frequire-nonnull-else.html&title=Objects.requireNonNullElse%28%29%20%E2%80%93%20java.evolved |
| Let us know. | https://github.com/javaevolved/javaevolved.github.io/issues/new?template=code-issue.yml&title=%5BCode%20Issue%5D%20Objects.requireNonNullElse%28%29&category=errors&slug=require-nonnull-else |
| Objects.requireNonNullElse() ↗ | https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Objects.html#requireNonNullElse(T,T) |
| Objects.requireNonNullElseGet() ↗ | https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Objects.html#requireNonNullElseGet(T,java.util.function.Supplier) |
| View proof source ↗ | https://github.com/javaevolved/javaevolved.github.io/blob/main/proof/errors/RequireNonnullElse.java |
|
Errors
Beginner
Helpful NullPointerExceptions
Java 8
// Old NPE message:
// "NullPointerException"
// at MyApp.main(MyApp.java:42)
// Which variable was null?!
Java 14+
// Modern NPE message:
// Cannot invoke "String.length()"
// because "user.address().city()"
// is null
// Exact variable identified!
Hover to see modern ➜
JDK 14+
→
| https://javaevolved.github.io/errors/helpful-npe.html |
|
Errors
Intermediate
Record-based error responses
Java 8
// Verbose error class
public class ErrorResponse {
private final int code;
private final String message;
// constructor, getters, equals,
// hashCode, toString...
}
Java 16+
public record ApiError(
int code,
String message,
Instant timestamp
) {
public ApiError(int code, String msg) {
this(code, msg, Instant.now());
}
}
Hover to see modern ➜
JDK 16+
→
| https://javaevolved.github.io/errors/record-based-errors.html |
|
Errors
Beginner
Optional chaining
Java 8
String city = null;
if (user != null) {
Address addr = user.getAddress();
if (addr != null) {
city = addr.getCity();
}
}
if (city == null) city = "Unknown";
Java 9+
String city = Optional.ofNullable(user)
.map(User::address)
.map(Address::city)
.orElse("Unknown");
Hover to see modern ➜
JDK 9+
→
| https://javaevolved.github.io/errors/optional-chaining.html |
| java.evolved | https://javaevolved.github.io/ |
| Bruno Borges | https://github.com/brunoborges |
| GitHub Copilot | https://github.com/features/copilot |
| modern-css.com | https://modern-css.com |
| View on GitHub | https://github.com/javaevolved/javaevolved.github.io |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow
URLs of crawlers that visited me.