I think happy-songs comment and link to a solution is the right direction. You'll want something like this to pass your test.

Optional.ofNullable(x).map(Object::toString).map(String::toLowerCase).orElse(null);
Answer from kparkinson on Stack Overflow
🌐
Oracle
oracle.com › java › technical details
Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional!
In this article, we have seen how ... single null reference in your codebase but rather to help design better APIs in which—just by reading the signature of a method—users can tell whether to expect an optional value....
🌐
amitph
amitph.com › home › java › avoid nullpointerexception using java optional
Avoid NullPointerException using Java 8 Optional | amitph
November 22, 2024 - A detailed guide with examples to null values and NullPointerExceptions, and using Java Optional to avoid NullPointerException and null checks
Discussions

Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional!
This means the result of the map operation is an object of type Optional>. As a result, the call to getUSB() is invalid because the outermost Optional contains as its value another Optional, which of course doesn't support the getUSB() method. Speaking of boilerplate, I guess I'll stick with my old trusty if (soundcard != null)... Maybe the code is neater this way, but somehow this approach seems a bit convoluted to me. More on reddit.com
🌐 r/programming
183
146
March 28, 2014
Using Optionals in Java 11 to avoid null pointer exceptions using functional chaining - Stack Overflow
I would like to use Java 11's Optional to protect myself from the dreaded null pointer exception. I wrote a basic test to check my understanding of how they work. like. so @Test public void More on stackoverflow.com
🌐 stackoverflow.com
option type - Java optional null pointer exception - Stack Overflow
My guess: isEnabled() returns a Boolean, and it's null. The ! operator then throws a NPE during unboxing. ... Your code works fine for me (once I provide a minimal Customer class for it to work with). The problem is not in what you present, and indeed, the stack trace should have already told you so. ... Thank you everyone... after the comments here I checked and it is indeed the isEnabled thats causing the NPE and not the optional... More on stackoverflow.com
🌐 stackoverflow.com
java - Optional of nullable on something giving NullPointerException - Stack Overflow
I m trying to use Optional.ofNullable on an a getter that is throwing a NullPointerException : ... The solution in Null check chain vs catching NullPointerException is very heavy. I try it in a jUnit test and takes several seconds to get the result! ... Save this answer. ... Show activity on this post. The variable individual is null and hence the exception... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @reetesh043 › java-optional-avoiding-null-pointer-exceptions-made-easy-d44e34b7c3e1
Java Optional: Avoiding Null Pointer Exceptions Made Easy | by Reetesh Kumar | Medium
February 9, 2024 - 2. Don’t use Optional as a return type from methods unless the absence of a value is a valid return value. For example, if we have a method that returns a user, we shouldn’t return an Optional<User>. Instead, we should return null if the user doesn’t exist.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-avoid-nullpointerexception-in-java-using-optional-class
How to avoid NullPointerException in Java using Optional class? - GeeksforGeeks
August 6, 2025 - String value is not present Note: Hence this can be understood as an exception handling method for NullPointerException NullPointerException Handling using Optional class: ... // Java program to handle NullPointerException // using Optional Class import java.util.Optional; public class Example { public static void main(String[] args) { // Create a String of size 10 String[] a = new String[10]; // Define the a[1] element a[1] = "geeksforgeeks"; // Create an Optional Class instance // and get the state for a[1] element // for Null value Optional<String> check = Optional.ofNullable(a[1]); // If t
🌐
CodeCurated
codecurated.com › blog › avoiding-the-null-pointer-exception-with-optional-in-java
Avoiding the Null Pointer Exception With Optional in Java
May 18, 2024 - Before we proceed any further, please note that I’m using Java 11 for the examples in this article. If you’re using a different version of Java, some methods might not exist or behave exactly the same. An empty optional is the main way to avoid the Null Pointer Exception when using the Optional API.
🌐
Reddit
reddit.com › r/programming › tired of null pointer exceptions? consider using java se 8's optional!
r/programming on Reddit: Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional!
March 28, 2014 - In java code that you need every drop of performance from, you'll probably switch back to null as an indication of a lack of data, but for your standard java code Optional should be more than performant enough while reducing the chance for errors caused by missing a null check somewhere or null propagation. Using the same techniques in my scala code has made null pointer exceptions pretty rare even in the very first versions of my applications.
Find elsewhere
🌐
DZone
dzone.com › coding › java › java 8 optional - avoid null and nullpointerexception altogether - and keep it pretty
Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
September 27, 2014 - There are other ways to do it (map + flatMap to Optional::ofNullable is one). The best one: only return optional value where it makes sense: if you know the value will always be provided, make it non-optional.
🌐
Tech with Maddy
techwithmaddy.com › how-to-handle-nullpointerexception-using-optional-class
How to Handle NullPointerException using Optional class
March 10, 2024 - In the if...else block, you're seeing if the country is present in the Optional or not. If it is, we print out the country in uppercase. Otherwise, we print a message saying, "The country is null." Since we haven't initialized the String array, the output would be: ... import java.util.Optional; public class Country { public static void main(String[] args) { String[] countries = new String[5]; countries[3] = "India"; Optional<String> checkCountry = Optional.ofNullable(countries[3]); if(checkCountry.isPresent()){ String country = countries[3].toUpperCase(); System.out.println(country); } else { System.out.println("The country is null"); } } }
🌐
Stack Overflow
stackoverflow.com › questions › 43374099 › java-optional-null-pointer-exception
option type - Java optional null pointer exception - Stack Overflow
My guess: isEnabled() returns a Boolean, and it's null. The ! operator then throws a NPE during unboxing. ... Your code works fine for me (once I provide a minimal Customer class for it to work with). The problem is not in what you present, and indeed, the stack trace should have already told you so. ... Thank you everyone... after the comments here I checked and it is indeed the isEnabled thats causing the NPE and not the optional...
🌐
DEV Community
dev.to › arpitmandliya › java-8-optional-a-way-to-avoid-nullpointerexception-10g1
Java 8 Optional: A way to avoid NullPointerException - DEV Community
April 11, 2019 - Do you see the issue here? We forgot to check null when we tried to find employee in the list. This occurs more often when you call library function and it returns null and you forget to check it. You can use Optional to solve this problem. You can change "JavaOptionalMain" as below.
🌐
Medium
medium.com › better-programming › avoiding-the-null-pointer-exception-with-optional-in-java-96cd75d9b058
Avoiding the Null Pointer Exception With Optional in Java | by Brilian Firdaus | Better Programming
October 6, 2020 - Before we proceed any further, please note that I’m using Java 11 for the examples in this article. If you’re using a different version of Java, some methods might not exist or behave exactly the same. An empty optional is the main way to avoid the Null Pointer Exception when using the Optional API.
🌐
Beyondjava
beyondjava.net › optionals-guidelines
Why Optional does not Solve the NullPointerException ...
July 21, 2020 - Beyond Java · Sitemap · Talks & Articles · Projects · Guest Posts · About · Legalese · Statistics · (opt out) · Mastodon
🌐
HackerEarth
hackerearth.com › practice › notes › tired-of-null-pointer-exceptions-consider-using-java-se-8s-optional
Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional! - Yogendra Gadilkar
A wise man once said you are not a real Java programmer until you've dealt with a null pointer exception. Joking aside, the null reference is the source of many problems because it is often used to denote the absence of a value. Java SE 8 introduces a new class called java.util.Optional that can alleviate some of these problems.
🌐
Stack Overflow
stackoverflow.com › questions › 42516598 › null-pointer-exception-with-optional
nullpointerexception - Null Pointer Exception with optional - Stack Overflow
April 29, 2017 - BottomObj bottomObj = new bottomObj();//initialized bottomObj Optional<Location> location = Optional.ofNullable(bottomObj).map(obj -> obj.getRootObj()).map(rootObjLocation -> rootObjLocation.getLocation()); A BottomObj has a RootObj instance variable that is null in this case(as I have not set it and the constructor doesn't set it). ... I would get a null pointer exception.
🌐
DEV Community
dev.to › vikneswaran › mitigate-null-pointer-exceptions-in-java-with-optional-class-52od
Mitigate Null Pointer Exceptions in Java with Optional class - DEV Community
September 23, 2019 - There are various methods which also comes with Optional class. /* Optional.get() It returns the wrapped object. it throws a no such element exception if the wrapped object is null.
🌐
DEV Community
dev.to › jarjanazy › how-to-escape-nullpointerexceptions-in-java-using-optional-pek
How to escape NullPointerExceptions in Java using Optional - DEV Community
March 24, 2021 - NullPointerException is a symptom of poor design thoughts, Optional is for when the state is indeterminate even at runtime (and even then, returning null is a valid strategy). ... I enjoy learning and teaching, I am currently working as a Software ...
🌐
Quora
quora.com › How-can-Java-Optionals-help-avoid-null-pointer-exceptions-in-your-code
How can Java Optionals help avoid null pointer exceptions in your code? - Quora
Answer: Whilst “Optionals” may well do that, now you’ve just moved the problem from being obvious to non-obvious: you have to deal with “it isn’t set” instead of having a null value. What does your code do when the optional isn’t set to something sensible?