A couple of tidbits about null pointers in Java

As far as I know, pointer dogs can’t point at null. Photo by Gui Avelar on Unsplash

One of the many examples of how Java and JavaScript differ is that in JavaScript, you can have your program throw anything: numbers, strings, promises, HTML form elements, etc. And of course instances of Error.

In Java, generally, you would have your program throw something subclassed from Exception, maybe Error in some rare cases.

Although it would usually be very bad form, you could even have your program throw an instance of Throwable (despite the name, it’s not an interface, and it’s not an abstract class either). Throwable is the superclass of Error and Exception.

Sure you can write things like “throw 13;” or “throw new ActionMap();” in a Java source file, but the program won’t compile. In an integrated development environment (IDE) like NetBeans or IntelliJ, you’d get a bright red indicator.

In my article “To be fair to JavaScript…” I characterized throwing null in JavaScript as “the height of absurdity.”

However, as I think more about it, I worry I might have left people with the impression that you can’t throw null in Java. Because actually, you can.

Wrap that in a class, it should compile. Then run it on the command line, you should get something like this:

What happened here is that the Java Virtual Machine converts null to a NullPointerException, which is subclassed from RuntimeException. This is exactly what should happen according to the Javadoc.

This also holds true in Scala, which is built on top of Java, as this Scastie snippet should confirm. I presume much the same can happen in Kotlin, which, like Scala, is also built on top of Java.

So this might seem like a neat way to throw a NullPointerException with a little less typing.

But note that the detail message “null” is practically useless (that’s the same exception message you get if you construct the exception without arguments — without parentheses in Scala).

If you don’t have the stack trace for a NullPointerException, it would be hard to know what caused the problem if the exception arises when you weren’t expecting it (presumably you can always get at the stack trace in your IDE).

The thing about the NullPointerException is that it can occur for situations other than those in which a non-null object was expected, such as trying to access the elements of an array of primitives that has not actually been initialized (the missing initialization has to be in some way hidden for the compiler to not catch it).

If you need to have your program throw a NullPointerException, it would be a good idea to construct the exception with a detail message that gives some kind of clue as to what went wrong.

I can’t think of any practical reason you’d want your Java program to throw null, other than to see that it can be done. But if you can think of such a reason, please let me know in the comments.

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store