Avoid NullPointerExceptions by using the Optional container.
`NullPointerException` is one of the most common and frustrating errors in Java. It occurs when you try to access a member of a null reference. The `Optional<T>` class, introduced in Java 8, is a container object designed to address this problem by explicitly modeling the presence or absence of a value. An `Optional` can be in one of two states: it can be 'present', containing a non-null value of type `T`, or it can be 'empty'. The main goal of `Optional` is to change a method's signature to clearly communicate that it might not return a value. Instead of a method returning `null`, it can return an `Optional`. For example, a method `findUserById(String id)` that might not find a user can be declared to return `Optional<User>`. The calling code is then forced to deal with the possibility of an absent value. `Optional` provides several methods to handle the value. The `isPresent()` method checks if a value is present. `get()` returns the value if present, but throws an exception if it's empty. Safer alternatives include `orElse(defaultValue)`, which returns the contained value if present, or a specified default value otherwise. `ifPresent(consumer)` executes a block of code only if the value is present. Using `Optional` makes your code more robust and readable by making the cases where a value may be missing explicit, pushing the developer to handle them actively rather than relying on (and often forgetting) null checks.