1
我正在寫一個簡單的IRC機器人,試圖瞭解更多關於Clojure的,使用標準的Java插座和一個循環讀取/分析數據,但我得到一個空指針時由於某種原因,我試圖在MOTD結束消息後發送JOIN消息。空指針異常
這裏是我的代碼..
(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 [email protected]
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)noob,我可能做了一些愚蠢的事情。
任何人有任何想法?
編輯:如果它的事項,我使用的Java 7和Clojure的1.4.0
可以使其更難意外做到這一點,通過遵循括號內的標準口齒不清約定。如果沒有括號是以往任何時候都對自己的路線,這幾乎是不可能意外地寫,而不是(富巴)((富巴)),如果你這樣做是非常明顯和容易修復。 – amalloy
@amalloy我喜歡你關於定位括號的答案,並已被告知有他們步道末。 – octopusgrabbus
@amalloy智能思維存在;我會牢記這一點。謝謝。 – gdude2002