Helpful NullPointerException (JVM)

Well, that got me curious, and it’s easy to test so…

> JAVA_HOME=$OPENJDK14_HOME clj -J-XX:+ShowCodeDetailsInExceptionMessages
Clojure 1.10.1
user=> (System/getProperty "java.version")
"14"
user=> (require '[clojure.string :as str])
nil
user=> (str/lower-case nil)
Execution error (NullPointerException) at user/eval5 (REPL:1).
Cannot invoke "Object.toString()" because "s" is null
user=> (* nil 42)
Execution error (NullPointerException) at user/eval7 (REPL:1).
Cannot invoke "Object.getClass()" because "x" is null
user=> (* 42 nil)
Execution error (NullPointerException) at user/eval9 (REPL:1).
Cannot invoke "Object.getClass()" because "x" is null
user=> (def n nil)
#'user/n
user=> (n 1 2 3)
Execution error (NullPointerException) at user/eval12 (REPL:1).
Cannot invoke "clojure.lang.IFn.invoke(Object, Object, Object)" because the return value of "clojure.lang.Var.getRawRoot()" is null

I’d say that’s a wash because the exception messages are deduced from the Clojure runtime that is written in Java so it’s a bit cryptic in places.

EDIT: I dug a bit deeper and some of the cryptic aspects come from how the compiler generates chained function calls so I suspect it would slow Clojure down to refactor the generated code in ways that might improve the messages.

2 Likes