2

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

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
Siscia
  • 1,421
  • 1
  • 12
  • 29
  • You need to identify where the exception is thrown first. It actually looks like the (. traccia getTitle) call from here - if you remove that, does it work? – Adrian Mouat Nov 07 '11 at 10:18
  • Ok, thanks for answer, no if I remove (. traccia getTitle) doesn't work, and it doesn't work also if I remove evrythinghs except the declaration of the namespace, so i'm pretty sure that the problem is the lein background... Why you thinked that the error was in (. traccia getLine) ?? – Siscia Nov 07 '11 at 13:59
  • Ah, I thought there was no title printed. – Adrian Mouat Nov 07 '11 at 15:42
  • "Rabiosa (Featuring Pitbull)" was the title XD – Siscia Nov 07 '11 at 20:14
  • Yeah, to my eternal shame I thought the artist was Shakira Rabiosa ;) – Adrian Mouat Nov 07 '11 at 20:41

1 Answers1

0

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.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102