0
votes

I am struggling with a NPE in my first Clojure program and would like some help.

call-elem is a struct which contains:-

  • :id, a string like some.packagename.SomeClass.someMethod
  • :calls (children), a vector of other call-elem structs

is defined as follows:

(defstruct call-struct :id :calls)

(defn- class-name [call]
  (let [id (call :id)]
    (.substring id 0 (.lastIndexOf id "."))))

(defn- method-name [call]
  (let [id (call :id)]
    (.substring id (inc (.lastIndexOf id ".")))))

I also have a method foo which takes a class-map and a call-elem as arguments. class-map is a map of class-name to method-map. method-map is a map of method-name to a list of variations. Each variation is a map with keys :class-name, :method-name and :calls. :calls is a list of other variations. The foo function should return a vector with two elements:-

  • the first element is a class-map with new entries corresponding to the call-elem and its children
  • the second element is a variation map corresponding to the argument call-elem

Here is the code:

(declare foo' add-new-variation)

(defn foo [class-map call-elem]
  (let [children (call-elem :calls)
        class-name (class-name call-elem)
        method-name (method-name call-elem)]
    (if (empty? children)
      (let [new-variation {:class-name class-name
                           :method-name method-name
                           :calls []}
            new-class-map (add-new-variation class-map 
                                             class-name method-name 
                                             new-variation)]
        [new-class-map new-variation])
      (let [[new-class-map child-variations-list] 
            (reduce #(foo' %1 %2) (class-map '()) children)
            new-variation {:class-name class-name
                           :method-name method-name
                           :calls child-variations-list}
            new-class-map' (add-new-variation new-class-map 
                                              class-name method-name 
                                              new-variation)]
        [new-class-map' new-variation]))))

(defn foo' [[class-map variations-list] call-elem]
  (let [[new-class-map new-variation] (foo class-map call-elem)]
    [new-class-map (cons new-variation variations-list)]))

(defn add-new-variation [class-map class-name method-name variation]
  (let [method-map (if (contains? class-map class-name)
                     (class-map class-name) {})
        variations-list (if (contains? method-map method-name)
                          (method-map method-name) [])
        new-variations-list (conj variations-list variation)
        new-method-map (assoc method-map method-name new-variations-list)]
    (assoc class-map class-name new-method-map)))

When I try to run the following code:

(try
  (let [call-elem 
        (struct call-struct "Class1.method1" 
                [(struct call-struct "Class2.method1" [])])]
    (second (foo {} call-elem)))
  (catch Exception ex
         (.printStackTrace ex)))

I get the following result:

{:class-name "Class1",
 :method-name "method1",
 :calls ({:class-name "Class2", :method-name "method1", :calls []})}

But when I try to run the following code, increasing the calls one level deeper, I get a NullPointerException:

(try
  (let [call-elem 
        (struct call-struct "Class1.method1" 
                [(struct call-struct "Class2.method1"
                         [(struct call-struct "Class3.method1" [])])])]
    (second (foo {} call-elem)))
  (catch Exception ex
         (.printStackTrace ex)))

Here is the stacktrace:

java.lang.NullPointerException
    at first.simple$foo.invoke(NO_SOURCE_FILE:46)
    at first.simple$foo_SINGLEQUOTE_.invoke(NO_SOURCE_FILE:55)
    at first.simple$foo$fn__1986.invoke(NO_SOURCE_FILE:46)
    at clojure.lang.ArrayChunk.reduce(ArrayChunk.java:58)
    at clojure.core.protocols$fn__5565.invoke(protocols.clj:30)
    at clojure.core.protocols$fn__5543$G__5538__5552.invoke(protocols.clj:11)
    at clojure.core$reduce.invoke(core.clj:5995)
    at first.simple$foo.invoke(NO_SOURCE_FILE:46)
    at first.simple$eval2010.invoke(NO_SOURCE_FILE:6)
    at clojure.lang.Compiler.eval(Compiler.java:6465)
    at clojure.lang.Compiler.eval(Compiler.java:6431)
    at clojure.core$eval.invoke(core.clj:2795)
    at clooj.repl$create_clojure_repl$repl_thread_fn__578$fn__589.invoke(repl.clj:147)
    at clojure.main$repl$read_eval_print__5967.invoke(main.clj:244)
    at clojure.main$repl$fn__5972.invoke(main.clj:265)
    at clojure.main$repl.doInvoke(main.clj:265)
    at clojure.lang.RestFn.invoke(RestFn.java:1523)
    at clooj.repl$create_clojure_repl$repl_thread_fn__578.invoke(repl.clj:145)
    at clojure.lang.AFn.run(AFn.java:24)
    at java.lang.Thread.run(Thread.java:680)
1

1 Answers

3
votes

Let's take a closer look at the reduce in foo. I suspect you're trying to pass a two-element collection as an initial value of reduce's accumulator. However using unquoted parentheses causes (class-map '()) to be treated as an expression to be evaluated. To make matters worse, it's valid, as class-map is a clojure.lang.PersistentArrayMap, which implementents IFn.

As a result, instead of creating a two element list you're calling (class-map '()), which most probably returns nil, as there's no '() key in class-map. Afterwards, in foo' you're trying to bind a nil to [class-map variations-list]. Boom. Null pointer exception.

Defining the second argument of reduce as a vector should solve the problem.

--- before  2011-12-27 12:52:43.052218334 +0100
+++ after   2011-12-27 12:52:56.785477270 +0100
@@ -13,7 +13,7 @@
                                              new-variation)]
         [new-class-map new-variation])
       (let [[new-class-map child-variations-list]
-            (reduce #(foo' %1 %2) (class-map '()) children)
+            (reduce #(foo' %1 %2) [class-map '()] children)
             new-variation {:class-name class-name
                            :method-name method-name
                            :calls child-variations-list}