I am new to the Guava library.
I am trying to use an option in method arguments. One of the problems that I discovered is that I cannot pass null to Optional.
I think the purpose of introducing the Optional is to distinguish between
- Something that doesn't matter
- That which has a null value
For example, Optional.absent () means that the value does not exist. So far, null is a value.
With this logic, I assume that the option must have some way to allow us to store a null value in it. However, I could not find a way to do this.
My method is defined as:
void myMethod(Optional<String> arguments) { .... }
If i use
myMethod(Optional.of(null));
This will give me a runtime error that the value cannot be null.
How can I pass null inside optional?
Kevin source share