Warm tip: This article is reproduced from serverfault.com, please click

How to debug a NullPointerException from lein run?

发布于 2020-11-29 15:11:45

I'm trying to create a simple Clojure app to parse stdin and execute some commands accordingly. I created a new leiningen project with lein new app myproject and added my code to src/myproject/core.clj :

(ns myproject.core
  (:require [clojure.string :as str])
  (:gen-class))

(def global-vector (ref [{:name "Bob" :age 22} {:name "Alice" :age 21}]))

(defn call [fn_name & fn_args]
  (apply (resolve (symbol fn_name)) (map keyword fn_args)))

(defn select
  [key]
  (dosync
    (map #(get % key) @global-vector)))

(defn -main []
  (while true
    (let [[command cargs] (str/split (read-line) #" ")]
    (println (call command cargs)))))

To be explicit here are the steps to reproduce what I'm trying to do:

  • I want to run this program from command line (currently I use lein run)
  • It should wait for user input (I use the read-line function)
  • I enter some text: select name
  • I expect the program to print the names stored in global-vector but it crashes with a NPE instead

Everything seems to work as expected when ran with lein repl:

myproject.core=> (select :name)
("Bob" "Alice")
myproject.core=> (apply select [:name])
("Bob" "Alice")
myproject.core=> (call "select" "name")
("Bob" "Alice")
myproject.core=> (let [[a b] (str/split "select name" #" ")]
        #_=>     (println (call a b)))
(Bob Alice)
nil
myproject.core=> (-main)
select name
(Bob Alice)

But when I run it with lein run and enter some input I get a NullPointerException:

$lein run
select name
Syntax error (NullPointerException) compiling at (/private/var/folders/lt/z56f492j3f3gcdf83y4f8x2r0000gn/T/form-init2363625704485841211.clj:1:125).
null

Full report at:
/var/folders/lt/z56f492j3f3gcdf83y4f8x2r0000gn/T/clojure-6381054570172229346.edn

Here is the content of the report file:

{:clojure.main/message
 "Syntax error (NullPointerException) compiling at (/private/var/folders/lt/z56f492j3f3gcdf83y4f8x2r0000gn/T/form-init2363625704485841211.clj:1:125).\nnull\n",
 :clojure.main/triage
 {:clojure.error/phase :compile-syntax-check,
  :clojure.error/line 1,
  :clojure.error/column 125,
  :clojure.error/source "form-init2363625704485841211.clj",
  :clojure.error/path
  "/private/var/folders/lt/z56f492j3f3gcdf83y4f8x2r0000gn/T/form-init2363625704485841211.clj",
  :clojure.error/class java.lang.NullPointerException},
 :clojure.main/trace
 {:via
  [{:type clojure.lang.Compiler$CompilerException,
    :message
    "Syntax error compiling at (/private/var/folders/lt/z56f492j3f3gcdf83y4f8x2r0000gn/T/form-init2363625704485841211.clj:1:125).",
    :data
    {:clojure.error/phase :compile-syntax-check,
     :clojure.error/line 1,
     :clojure.error/column 125,
     :clojure.error/source
     "/private/var/folders/lt/z56f492j3f3gcdf83y4f8x2r0000gn/T/form-init2363625704485841211.clj"},
    :at [clojure.lang.Compiler load "Compiler.java" 7648]}
   {:type java.lang.NullPointerException,
    :at [clojure.core$apply invokeStatic "core.clj" 665]}],
  :trace
  [[clojure.core$apply invokeStatic "core.clj" 665]
   [clojure.core$apply invoke "core.clj" 660]
   [myproject.core$call invokeStatic "core.clj" 8]
   [myproject.core$call doInvoke "core.clj" 7]
   [clojure.lang.RestFn invoke "RestFn.java" 423]
   [myproject.core$_main invokeStatic "core.clj" 18]
   [myproject.core$_main invoke "core.clj" 15]
   [clojure.lang.Var invoke "Var.java" 380]
   [user$eval140 invokeStatic "form-init2363625704485841211.clj" 1]
   [user$eval140 invoke "form-init2363625704485841211.clj" 1]
   [clojure.lang.Compiler eval "Compiler.java" 7177]
   [clojure.lang.Compiler eval "Compiler.java" 7167]
   [clojure.lang.Compiler load "Compiler.java" 7636]
   [clojure.lang.Compiler loadFile "Compiler.java" 7574]
   [clojure.main$load_script invokeStatic "main.clj" 475]
   [clojure.main$init_opt invokeStatic "main.clj" 477]
   [clojure.main$init_opt invoke "main.clj" 477]
   [clojure.main$initialize invokeStatic "main.clj" 508]
   [clojure.main$null_opt invokeStatic "main.clj" 542]
   [clojure.main$null_opt invoke "main.clj" 539]
   [clojure.main$main invokeStatic "main.clj" 664]
   [clojure.main$main doInvoke "main.clj" 616]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.main main "main.java" 40]],
  :phase :compile-syntax-check}}

I'm trying to understand the traceback but I lack familiarity with the Clojure/Java environment and it's indecipherable so far. What's being called on a Null value exactly? Is it because global-vector is defined outside of main? (But why does it work in Repl then?)

I use Clojure version: 1.10.1.739 and Leiningen 2.9.4 on Java 11.0.2 OpenJDK 64-Bit Server VM.

Questioner
Joucks
Viewed
0
Joucks 2020-11-30 00:40:16

After further investigation I found that it has to do with namespace resolution. The following expression:

(resolve (symbol fn_name))

works in Repl but return nil when executed from "lein run", which in turn causes the apply function to raise a NullPointerException.

The symbol function can be passed a ns parameter so I fixed it by hardcoding my current namespace like this:

(resolve (symbol "myproject.core" fn_name))