Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure NullPointerException when def a vector

Tags:

clojure

Something wrong with

(def b [08])

java.lang.NullPointerException: null

but

(def b [8])

is OK

why?

like image 945
procr Avatar asked Feb 10 '26 23:02

procr


2 Answers

I know nothing about clojure, but in many languages, an integer literal that starts with a zero is interpreted as octal (base 8). And 8 is an invalid octal digit.

From a quick experiment at Try Clojure:

> (def b [08])
java.lang.NumberFormatException: Invalid number: 08
> (def b [07])
#'sandbox155/b

It appears that this is indeed your problem.

Don't start integers with a leading zero (e.g. 08), unless you actually intend octal notation.

like image 190
Jonathon Reinhart Avatar answered Feb 15 '26 11:02

Jonathon Reinhart


@Jonathon Reinhart is right, according to the LispReader.java :(https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LispReader.java#L65) :

static Pattern intPat = Pattern.compile( "([-+]?)(?:(0)|([1-9][0-9]*)|0[xX]([0-9A-Fa-f]+)|0([0-7]+)|([1-9][0-9]?)[rR]([0-9A-Za-z]+)|0[0-9]+)(N)?");

The part 0([0-7]+) in the pattern prove this.

like image 39
llj098 Avatar answered Feb 15 '26 10:02

llj098



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!