1

我正在使用resolve给定名称(字符串)来调用函数,例如

(call "+" 10 10 10) ;; 30

call功能:

(defn call [this & that]
  (apply (resolve (symbol this)) that))

在 REPL 上一切正常。但是当我将项目作为server使用 httpkit http 服务器启动时,我收到了一个 NPE。

java.lang.NullPointerException
    at clojure.core$apply.invoke(core.clj:626)
    at tapahtuma.http_resources$call.doInvoke(http_resources.clj:41)
    at clojure.lang.RestFn.invoke(RestFn.java:423)
    at tapahtuma.http_resources$forwarder.invoke(http_resources.clj:61)
    at tapahtuma.http_resources$create_event.invoke(http_resources.clj:69)
    at compojure.response$eval1563$fn__1564.invoke(response.clj:26)
    at compojure.response$eval1524$fn__1525$G__1515__1532.invoke(response.clj:10)
    at compojure.core$make_route$fn__1699.invoke(core.clj:93)
    at compojure.core$if_route$fn__1683.invoke(core.clj:39)
    at compojure.core$if_method$fn__1676.invoke(core.clj:24)
    at compojure.core$routing$fn__1705.invoke(core.clj:106)
    at clojure.core$some.invoke(core.clj:2528)
    at compojure.core$routing.doInvoke(core.clj:106)
    at clojure.lang.RestFn.applyTo(RestFn.java:139)
    at clojure.core$apply.invoke(core.clj:628)
    at compojure.core$routes$fn__1709.invoke(core.clj:111)
    at tapahtuma.http_resources$wrap_content_type$fn__7192.invoke(http_resources.clj:22)
    at ring.middleware.cors$wrap_cors$fn__1800.invoke(cors.clj:47)
    at org.httpkit.server.HttpHandler.run(RingHandler.java:33)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

在第 41 行http_resources我有这个call功能。

我不知道这里发生了什么。感谢您在这里的任何帮助。我创建了一个项目来帮助重现问题

4

1 回答 1

2

对您的复制器的以下修改——完全限定命名空间——可以正常工作:

(defn call [this & that]
  (let [this-sym (symbol this)
        this-resolved (ns-resolve 'resolver-clj.core this-sym)]
     (.println System/out (str "current-ns: " *ns*))
     (.println System/out (str "this-sym: " this-sym))
     (.println System/out (str "this-resolved: " this-resolved))
     (apply this-resolved that)))

当您查看*ns*再现环境中的值时,原因很明显,即clojure.core.

于 2015-05-12T17:00:53.807 回答