CompilerException when using (q/color) in a def

17 views
Skip to first unread message

John Lynch

unread,
Sep 3, 2017, 11:42:50 AM9/3/17
to clj-processing
Attempting to compile the folowing code results in a

CompilerException java.lang.NullPointerException, compiling:(dynamic.clj:7:12)

Seemingly the compiler doesn't like (q/color) in a def, although as can be seen, it appears to be happy with (q/sqrt) in a def, and with (q/color) in a let.

This error is perplexing me a lot!   Can anyone help please?

(ns test42.dynamic
  (:require [quil.core :as q]
            [clojure.string :as str]))

(def ROOT2 (q/sqrt 2.0))

(def BLACK (q/color 0 0 0))

(defn setup []
  (q/no-stroke)
  (q/smooth)
  (q/frame-rate 256)
  (q/color-mode :hsb 255.0)
  (q/background 0))

(defn update [state])

(defn draw [state]
  (let [hue 127
        col (q/color hue 255 255)]))






Nikita Beloglazov

unread,
Sep 4, 2017, 3:45:06 PM9/4/17
to clj-processing
Hi John

Quil has 2 classes of functions. The first class is functions that can be called only inside :draw or :setup or :update. Basically anywhere where you can say "they need sketch to work properly". For example functions like q/smooth, q/ellipse. Function of the second class, on the other hand, you can call anywhere. They are static and don't need sketch to work. For example q/sqrt or q/abs belong to the second class. (because they are just math functions). 

q/color belongs to the first class which requires sketch to run. I agree it's counterintuitive and I think in the past I also assumed that q/color is static and got the same error as you did. As a workaround you can define black as [0 0 0] and then convert it to color when you actually need it inside setup/draw. Yes, it's less optimal but it's ok unless you need to optimize performance really hard for your sketch. 

To check whether a function can be used outside of sketch (e.g. with def like you want) you can check http://quil.info/api. For example for q/color it says on the right that it can work only inside sketch. 

Nikita

--
You received this message because you are subscribed to the Google Groups "clj-processing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-processin...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Lynch

unread,
Sep 4, 2017, 6:22:34 PM9/4/17
to clj-processing

 
Hi Nikita -
           
Thanks for your answer.

However, my workaround was to try defining it as a function:

(defn BLACK [] (q/color 0 0 0))

This works, despite not being inside the :draw or :setup or :update functions!

So why does it work???!

Regards, John.
_____________________________________________________________

Nikita Beloglazov

unread,
Sep 4, 2017, 6:26:20 PM9/4/17
to clj-processing
I'm guessing you are running this function from inside :draw, right? When Quil runs :draw it binds dynamic *applet* variable to actual sketch and when you call (BLACK) it reads *applet* which is now has actual sketch and not null. 

Reply all
Reply to author
Forward
0 new messages