I would recommend you never throw NullPointerException by yourself.

The main reason not to do this, as Thorbjørn Ravn Andersen says in a comment below, is that you don't wan't to mix 'real, bad NPEs' with NPEs thrown intentionally.

So, until you're confident that you're able to recognize 'valid' NPE, I'd recommend to use IllegalArgumentException when you want to tell to your API user that null is not a valid argument value. Your method's behavior when illegal null-parameter passed should be documented.

Another (more modern imho) option is to use @NotNull annotation near the argument. Here is an article about using @NotNull annotation.

As I mentioned earlier, there can also be cases, when throwing NPE will not be confusing either to you or to your teammates: NPE cause should be clear and recognizable.

For instance, if you use some library with preconditions module, like Guava, then I find using checkNotNull()-like methods is a preferable way to deal with illegally-passed nulls.

checkNotNull(arg, msg) throws NPE, but from the stacktrace it's quite clear, that it was produced by Preconditions.checkNotNull() and thus it's not an unknown bug but rather expected behavior.

Answer from Roman on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › NullPointerException.html
NullPointerException (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... Thrown when an application attempts to use null in a case where an object is required.
Top answer
1 of 16
74

I would recommend you never throw NullPointerException by yourself.

The main reason not to do this, as Thorbjørn Ravn Andersen says in a comment below, is that you don't wan't to mix 'real, bad NPEs' with NPEs thrown intentionally.

So, until you're confident that you're able to recognize 'valid' NPE, I'd recommend to use IllegalArgumentException when you want to tell to your API user that null is not a valid argument value. Your method's behavior when illegal null-parameter passed should be documented.

Another (more modern imho) option is to use @NotNull annotation near the argument. Here is an article about using @NotNull annotation.

As I mentioned earlier, there can also be cases, when throwing NPE will not be confusing either to you or to your teammates: NPE cause should be clear and recognizable.

For instance, if you use some library with preconditions module, like Guava, then I find using checkNotNull()-like methods is a preferable way to deal with illegally-passed nulls.

checkNotNull(arg, msg) throws NPE, but from the stacktrace it's quite clear, that it was produced by Preconditions.checkNotNull() and thus it's not an unknown bug but rather expected behavior.

2 of 16
44

I see no problem with throwing a NPE as early as possible before the JVM does it for you - in particular for null arguments. There seems to be some debate about this, but there are many examples in the Java SE libraries that does exactly this. I cannot see why NPE should be holy in the aspect that you are not able to throw it yourself.

However, I digress. This question is about something different. You are talking about a post-condition stating that the return value mustn't be null. Surely null in this case would mean you have a bug inside the very method?

How would you even document this? "This method throws a NullPointerException if the return value unexpectedly is null"? Without explaining how this could happen? No, I would use an assertion here. Exceptions should be used for errors that can conceivably happen - not to cover things that can happen if there's something wrong inside the method, because that does not help anybody.

🌐
Frankel
blog.frankel.ch › throwing-a-nullpointerexception-or-not
Throwing a NullPointerException... or not
September 28, 2014 - public void someMethod(Type parameter) { if (parameter == null) { throw new NullPointerException("Parameter Type cannot be null"); } // Rest of the method }
🌐
Rollbar
rollbar.com › home › how to catch and fix nullpointerexception in java
How to Catch and Fix NullPointerException in Java
June 30, 2026 - In Java, primitives cannot have null values. Writing methods that return empty objects rather than null where possible. For example, returning empty collections and empty strings from a method. To fix the NullPointerException in the earlier example, the String object should be checked for null or empty values before it is used any further:
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › NullPointerException.html
NullPointerException (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... Thrown when an application attempts to use null in a case where an object is required.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › null-pointer-exception-in-java
Null Pointer Exception in Java - GeeksforGeeks
1 week ago - Explanation: The variable s contains null, so it does not refer to a String object. When s.length() is called, Java cannot invoke the method on a non-existent object and throws NullPointerException.
🌐
Medium
supakon-k.medium.com › 4-ways-to-handle-null-objects-in-java-7e2596c235d
4 ways to handle NullPointerException in Java | by Supakon_k | Medium
August 5, 2022 - This way also make you can do some processes before throwing the exception. ... We can write more shortly code by using short if. A simple one-line code and easy to read. ... In standard java.util, we have an Object class that can verify null objects. When assigning value to the object and checking exceptions simultaneously, it is easy to use. ... String result = list.stream() .filter(value -> value.contains("A")) .findFirst() .orElseThrow(() -> new NullPointerException("data is null"));
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-lang-nullpointerexception
Java NullPointerException - Detect, Fix, and Best Practices | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
TutorialsPoint
tutorialspoint.com › why-can-i-throw-null-in-java-and-why-does-it-upcast-to-a-nullpointerexception
Why can I throw null in Java and why does it upcast to a NullPointerException?
July 2, 2020 - According to Java documentation a NullPointerException occurs if you try to − · Call the a method (instance) using null object. Access, modify, print, field of a null value (object). Trying to access (print/use in statements) the length of null value. Throw a null value.
🌐
Medium
medium.com › developers-journal › how-to-handle-null-pointer-exception-in-java-776ab2e0d8f5
How to Handle Null Pointer Exception in Java | by DJ | Developers Journal | Medium
January 27, 2022 - Throwing null, as if it were a Throwable value. When you try to synchronize over a null object. The NullPointerException is a RuntimeException and thus, the Javac compiler does not force you to use a try-catch block to handle it appropriately.
🌐
How to do in Java
howtodoinjava.com › home › exception handling › java nullpointerexception
Handling Java NullPointerException and Best Practices
October 1, 2022 - Joshua bloch in effective java says that “Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states. If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException.”
🌐
freeCodeCamp
freecodecamp.org › news › how-to-handle-nullpointerexception-in-java
How to Handle NullPointerException in Java
July 13, 2020 - Also when we call this function, we put a try-catch block around the function call to catch NullPointerException. If any of the arguments given in the function turn out to be null, the function would throw a NullPointerException.
🌐
Scaler
scaler.com › home › topics › null pointer exception in java
Null Pointer Exception in Java - Scaler Topics
March 20, 2024 - As we discussed, the null pointer exception in Java is a runtime exception, which is why it extends from the runtime exception class. The runtime exception class inherits the exception class. Throwable is a subclass of the object class, and the exception class is derived from the throwable class.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › what is nullpointerexception in java & how to avoid it
What Is NullPointerException In Java & How To Avoid It
April 1, 2025 - Exception class in turn is derived from the Throwable class that is a subclass of Object. Now we will demonstrate each of the scenarios of NullPointerException occurrence that we listed above. ... Consider the following code example. Here we have a class, MyClass that provides two methods. The first method ‘initT’ returns a null object. In the main method, we create an object of MyClass with a call to the initT method. Next, we call the print method of MyClass. Here, the java.lang.NullPointerException is thrown as we are calling the print method using a null object.
🌐
Educative
educative.io › answers › how-to-resolve-the-javalangnullpointerexception
How to resolve the java.lang.NullPointerException
In Java, the java.lang.NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object.
🌐
Coderanch
coderanch.com › t › 523633 › java › Shortcut-throwing-NullPointerException
Shortcut for throwing a NullPointerException (Java in General forum at Coderanch)
Dennis Grimbergen wrote:Is it because the throw keyword expects a Throwable to be specified, which is null in this case, therefore resulting in a NullPointerException? Yes, I'd say you got that right. If I were supervising the programmer who wrote that I would (1) congratulate them on finding a cute trick like that (2) tell them to change it to actually throw a NullPointerException.