Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Objects#requireNonNull

Tags:

After checking the JavaDocs for a method I was thinking of using, requiredNonNull, I stumbled across the first one with the single parameter (T obj).

However what is the actual purpose of this particular method with this signature? All it simply does is throw and NPE which I'm somewhat positive (as a I may be missing something obvious here) would be thrown anyway.

Throws: NullPointerException - if obj is null


The latter actually makes sense in terms of debugging certain code, as the doc also states, it's primarily designed for parameter validation

public static <T> T requireNonNull(T obj,String message)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

Therefore I can print specific information along with the NPE to make debugging a hell of a lot easier.


With this in mind I highly doubt I would come across a situation where I'd rather just use the former instead. Please do enlighten me.

tl;dr - Why would you ever use the overload which doesn't take a message.

like image 838
Juxhin Avatar asked Dec 16 '14 18:12

Juxhin


2 Answers

A good principle when writing software is to catch errors as early as possible. The quicker you notice, for example, a bad value such as null being passed to a method, the easier it is to find out the cause and fix the problem.

If you pass null to a method that is not supposed to receive null, a NullPointerException will probably happen somewhere, as you already noticed. However, the exception might not happen until a few methods further down, and when it happens somewhere deep down, it will be more difficult to find the exact source of the error.

So, it's better when methods check their arguments up front and throw an exception as soon as they find an invalid value such as null.

edit - About the one-parameter version: even though you won't provide an error message, checking arguments and throwing an exception early will be more useful than letting the null pass down until an exception happens somewhere deeper down. The stack trace will point to the line where you used Objects.requireNonNull(...) and it should be obvious to you as a developer that that means you're not supposed to pass null. When you let a NullPointerException happen implicitly you don't know if the original programmer had the intent that the variable should not be null.

like image 185
Jesper Avatar answered Oct 11 '22 23:10

Jesper


It is a utility method. Just a shortcut! (shortcut designers have their ways of doing their shortcut style).

Why throwing in the first place? Security and Debugging.

  • Security: to not allow any illegal value in a sensitive place. (makes inner algorithm more sure about what are they doing and what are they having).
  • Debugging: for the program to die fast when something unexpected happens.
like image 31
LSafer Avatar answered Oct 11 '22 23:10

LSafer