NullPointerException clojure println
-
22-02-2021 - |
Question
I'm learning clojure and I found a problem in this very ugly, anti-idiomatic, stupid code that I wrote:
(ns music-tag.core
(:import
(java.io.File)
(com.echonest.api.v4.EchoNestAPI)
(com.echonest.api.v4.Track)))
(def api-key "JRZSJUMBK8VOOP0L2")
(def music (new java.io.File "/home/simo/Musica/musica_mp3/Rabiosa-Shakira.mp3"))
(def echo-nest (new com.echonest.api.v4.EchoNestAPI api-key))
(def traccia (. echo-nest uploadTrack music true))
(. traccia waitForAnalysis 30)
(do (println (. traccia getArtistName) (. traccia getTitle)))
When I run this file I get the right answer (print artist and title) but it throws a exception:
simo@simo:~/music-tag$ lein run
Shakira Rabiosa (Featuring Pitbull)
Exception in thread "main" java.lang.NullPointerException
at user$eval39.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:6465)
at clojure.lang.Compiler.eval(Compiler.java:6455)
at clojure.lang.Compiler.eval(Compiler.java:6431)
at clojure.core$eval.invoke(core.clj:2795)
at clojure.main$eval_opt.invoke(main.clj:296)
at clojure.main$initialize.invoke(main.clj:315)
at clojure.main$null_opt.invoke(main.clj:348)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:405)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Why? How can I solve it???
Thanks
Solution
I'm not sure, but I think it's just because there is no main method defined.
Try changing your code from the last def to:
(defn -main[]
(let [traccia (. echo-nest uploadTrack music true)]
(do
(. traccia waitForAnalysis 30)
(println (. traccia getArtistName) (. traccia getTitle)))))
When lein compiles your code, it needs to run the top-level statements, which includes your method calls as they aren't hidden in a function. When it comes to running your code, it barfs with an exception as it has nothing to run.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow