我正在编写一个简单的IRC bot来尝试更多地了解Clojure,使用标准Java套接字和循环来读取/解析数据,但是当我尝试在MOTD结束后发送JOIN消息时,我得到一个nullpointer消息由于某种原因。
这是我的代码..
(use 'clojure.java.io
'[clojure.string :as s :only (split)])
(def sock (java.net.Socket. "iris.archivesmc.com" 6667))
(def write-stream (writer sock))
(def read-stream (reader sock))
(defn write [message]
(.write write-stream (str message "\r\n"))
(println (str "-> " message))
(.flush write-stream)
)
(defn parseMessage [buffer]
(if
(= (apply str (take-last 2 buffer)) "\r\n" )
(do
(let [parts (split buffer #"\s")]
(println (s/replace buffer "\r\n" ""))
(when
(= (first parts) "PING")
;; We need to PONG to this or we'll ping out
(write (str "PONG " (second parts)))
)
(when
(= (apply str(take 1 (first parts))) ":" )
;; Message with a numerical prefix
;; :server numeric our-nick message
(
(if
(= (second parts) "376")
;; MOTD end
;; Join #clojure
(write "JOIN #clojure") ;; This errors for some reason
)
)
)
)
""
)
buffer
)
)
(defn readLoop []
(loop [buffer ""]
;;(println (str "Looping.. -> " buffer))
(let [nbuf (parseMessage buffer)
nchr (.read read-stream) ]
(if-not (= nchr -1)
(recur (str nbuf (char nchr)))
)
)
)
)
(write "NICK gClojureTestBot")
(write "USER Clojure Imma Bot :Clojure Testing Bot")
(readLoop)
这是我的输出(带回溯等)
/usr/lib/jvm/jdk1.7.0_04/bin/java -Didea.launcher.port=7542 -Didea.launcher.bin.path=/home/gcoles/Downloads/idea-IC-117.418/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/jdk1.7.0_04/jre/lib/resources.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/javaws.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/jfr.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/jsse.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/deploy.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/management-agent.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/jce.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/charsets.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/plugin.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/rt.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/ext/sunec.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/ext/zipfs.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/ext/dnsns.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/ext/localedata.jar:/usr/lib/jvm/jdk1.7.0_04/jre/lib/ext/sunpkcs11.jar:/home/gcoles/IdeaProjects/ClojureBot/classes:/home/gcoles/clojure/clojure-1.4.0/clojure-1.4.0.jar:/home/gcoles/IdeaProjects/ClojureBot/src:/home/gcoles/IdeaProjects/ClojureBot/test:/home/gcoles/Downloads/idea-IC-117.418/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain clojure.main /home/gcoles/IdeaProjects/ClojureBot/src/main.clj
-> NICK gClojureTestBot
-> USER Clojure Imma Bot :Clojure Testing Bot
PING :5D2E1C22
-> PONG :5D2E1C22
:iris.archivesmc.com 001 gClojureTestBot :Welcome to the archivesmc.com IRC Network gClojureTestBot!~Clojure@213.190.134.190
Exception in thread "main" java.lang.NullPointerException
at user$parseMessage.invoke(main.clj:35)
at user$readLoop.invoke(main.clj:49)
at user$eval10.invoke(main.clj:60)
at clojure.lang.Compiler.eval(Compiler.java:6511)
at clojure.lang.Compiler.load(Compiler.java:6952)
at clojure.lang.Compiler.loadFile(Compiler.java:6912)
at clojure.main$load_script.invoke(main.clj:283)
at clojure.main$script_opt.invoke(main.clj:343)
at clojure.main$main.doInvoke(main.clj:427)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:415)
at clojure.lang.AFn.applyToHelper(AFn.java:161)
at clojure.lang.Var.applyTo(Var.java:532)
at clojure.main.main(main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 1
我觉得这很奇怪,因为之前对write
的调用正在发挥作用。然后,作为一个Clojure(和Java)菜鸟,我可能做了一些愚蠢的事情。
有人有任何想法吗?
编辑:如果重要,我正在使用Java 7和Clojure 1.4.0
答案 0 :(得分:1)
问题在于以下代码块。
(when
(= (apply str(take 1 (first parts))) ":" )
;; Message with a numerical prefix
;; :server numeric our-nick message
(
(if
(= (second parts) "376")
;; MOTD end
;; Join #clojure
(write "JOIN #clojure") ;; This errors for some reason
)
)
)
具体而言,()
声明周围的(if)
加倍。支架太多了。删除额外的问题可以解决问题。