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
🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Preventing_NullPointerException
Preventing NullPointerException - Wikibooks, open books for an open world
NullPointerException is a RuntimeException. In Java, a special null can be assigned to an object reference. NullPointerException is thrown when an application attempts to use an object reference, having the null value.
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.

🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › NullPointerException.html
NullPointerException (Java Platform SE 8 )
April 21, 2026 - Thrown when an application attempts to use null in a case where an object is required.
🌐
Rollbar
rollbar.com › home › how to catch and fix nullpointerexception in java
NullPointerException Crash Your Java App? Here's How to Fix It
November 29, 2025 - Here is an example of a NullPointerException thrown when the length() method of a null String object is called:
🌐
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.”
🌐
Sentry
sentry.io › sentry answers › java › avoiding `nullpointerexception` in java
Avoiding `NullPointerException` in Java | Sentry
July 12, 2022 - Therefore, a null pointer exception will occur when the code is pointing to something in memory that does not exist. The Java API documentation on NullPointerException lists a couple of scenarios where this exception could be invoked:
🌐
Sentry
sentry.io › sentry answers › java › what is a nullpointerexception, and how do i fix it?
What is a NullPointerException, and how do I fix it? | Sentry
A NullPointerException in Java is one of the most common errors. It means that you are trying to access a part of something that doesn’t exist. For example, in the code below we call .length() on myString, which would usually return the length of the string.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › null-pointer-exception-in-java
Null Pointer Exception in Java - GeeksforGeeks
April 24, 2026 - It occurs when a program attempts to use an object reference that has the null value. In Java, "null" is a special value that can be assigned to object references to indicate the absence of a value.
🌐
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 - In the above example program, instead of throwing a valid object, null is thrown. This results in Null Pointer Exception.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-lang-nullpointerexception
Java NullPointerException - Detect, Fix, and Best Practices | DigitalOcean
August 3, 2022 - NullPointerException is being thrown in statement int i = t.x; because “t” is null here. public class Temp { public static void main(String[] args) { foo(null); } public static void foo(String s) { System.out.println(s.toLowerCase()); } } This is one of the most common occurrences of java.lang.NullPointerException because it’s the caller who is passing the null argument. The below image shows the null pointer exception when the above program is executed in Eclipse IDE.
🌐
Frankel
blog.frankel.ch › throwing-a-nullpointerexception-or-not
Throwing a NullPointerException... or not
September 28, 2014 - This was very clear in my head, until the dev pointed me the NullPointerException Javadoc. For simplicity’s sake, here’s the juicy part: Thrown when an application attempts to use null in a case where an object is required.
🌐
DEV Community
dev.to › sharmaprash › what-is-a-nullpointerexception-and-how-do-i-fix-it-1j3i
What is a NullPointerException, and how do I fix it? - DEV Community
November 21, 2024 - A Null Pointer Exception (NPE), represented as java.lang.NullPointerException, occurs when a Java program attempts to use a null reference where an object is required.
🌐
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. This error can be resolved by using a try-catch block or an if-else condition to check if a reference ...
🌐
Sonar Community
community.sonarsource.com › rules and languages › report false-positive / false-negative...
squid:S2259 : A "NullPointerException" could be thrown; "dc" is nullable here. While "dc" is checked as not null - Report False-positive / False-negative... - Sonar Community
September 17, 2018 - Hello. We have a false positive with the " Null pointers should not be dereferenced -squid:S2259" rule : We have a “NullPointerException” false positive for which we do not know how to solve it. In the code, we check the not nullity with java.util.Objects.nonNull(). But even with this, the concerned object it tagged as a possible NullPointerException problem.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.nullpointerexception
NullPointerException Class (Java.Lang) | Microsoft Learn
[<Android.Runtime.Register("java/lang/NullPointerException", DoNotGenerateAcw=true)>] type NullPointerException = class inherit RuntimeException ... Thrown when an application attempts to use null in a case where an object is required. These ...
🌐
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 - But, you cannot use an object with null value or (a null value instead of an object) if you do so, a NullPointerException will be thrown.