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 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

4

1 回答 1

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)声明翻了一番。括号太多。删除多余的解决了这个问题。

于 2012-06-22T11:05:55.020 回答