Errors Beginner

Replace nested null checks with an Optional pipeline.

โœ• 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");
See a problem with this code? Let us know.
๐Ÿ”—

Chainable

Each .map() step handles null transparently.

๐Ÿ“–

Linear flow

Read left-to-right instead of nested if-blocks.

๐Ÿ›ก๏ธ

NPE-proof

null is handled at each step โ€” no crash possible.

Old Approach
Nested Null Checks
Modern Approach
Optional Pipeline
Since JDK
9
Difficulty
Beginner
Optional chaining
Available

Available since JDK 8+ (improved in 9+)

Optional.map() chains through nullable values, short-circuiting on the first null. orElse() provides the default. This eliminates pyramid-of-doom null checking.

Share ๐• ๐Ÿฆ‹ in โฌก