Is there any framework that can throw exception if I pass null as parameter to @NotNull
annotation? I don't mean static analysis but run-time checks.
If not, how to implement it?
If you are using Java 6 or a lower version then you can use Guava
Preconditions
.
Preconditions.checkNotNull(param);
However, if you using Java 7
or a higher version then there is a Utility method in Objects
.
Objects.requireNonNull(param);
And there is an overload that takes string to add a message to the NullPointerException
that will be thrown
Objects.requireNonNull(param,"Param cannot be null");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With