Welcome! Please see the About page for a little more info on how this works.

0 votes
in Errors by

Bumped into this quirk when repling around today: (filter zero? [:hi]) throws a java.lang.ClassCastException without a proper stack trace.

I am using Clojure version 1.11.1.1347 .

1 Answer

+1 vote
by
selected by
 
Best answer

The title is misleading - there's no NPE here, only class cast exception.

There is a stack trace (notice how I used *e in my REPL to print the latest exception):

Clojure 1.11.1
user=> (filter zero? [:hi])
Error printing return value (ClassCastException) at clojure.lang.Numbers/isZero (Numbers.java:119).
class clojure.lang.Keyword cannot be cast to class java.lang.Number (clojure.lang.Keyword is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')
(user=> 
user=> *e
#error {
 :cause "class clojure.lang.Keyword cannot be cast to class java.lang.Number (clojure.lang.Keyword is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')"
 :via
 [{:type clojure.lang.ExceptionInfo
   :message nil
   :data #:clojure.error{:phase :print-eval-result}
   :at [clojure.main$repl$read_eval_print__9206 invoke "main.clj" 442]}
  {:type java.lang.ClassCastException
   :message "class clojure.lang.Keyword cannot be cast to class java.lang.Number (clojure.lang.Keyword is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')"
   :at [clojure.lang.Numbers isZero "Numbers.java" 119]}]
 :trace
 [[clojure.lang.Numbers isZero "Numbers.java" 119]
  [clojure.core$zero_QMARK_ invokeStatic "core.clj" 874]
  [clojure.core$zero_QMARK_ invoke "core.clj" 869]
  [clojure.core$filter$fn__5962 invoke "core.clj" 2834]
  [clojure.lang.LazySeq sval "LazySeq.java" 42]
  [clojure.lang.LazySeq seq "LazySeq.java" 51]
  [clojure.lang.RT seq "RT.java" 535]
  [clojure.core$seq__5467 invokeStatic "core.clj" 139]
  [clojure.core$print_sequential invokeStatic "core_print.clj" 53]
[...]

So it seems that either the tools that you're using with Clojure hide it from you for some reason (in which case knowing the exact steps you perform to not get a stacktrace would be helpful) or you didn't look in the right place.

And in case you mean that the stacktrace seems to be pointing away from where you call filter - that's because filter doesn't actually filter anything, it creates a lazy seq that filters things out when it's being iterated over. So the stacktrace will might not include where filter is called but will include where the resulting lazy seq is being realized.

by
Sorry, I used different versions of Clojure in Emacs REPL and terminal. The error was actually created with version 1.10.0, I get a much better behavior after bumping up to 1.11.1 , with a proper stack trace. (I said null pointer error because it just printed "null" in the REPL when evaluating the expression, sorry for the misconception there).
...