1

I am getting the following stacktrace when running the command: lein run "this is the other different thing" "this,different,other"

Stacktrace

Exception in thread "main" java.lang.NullPointerException, compiling:(/private/var/folders/y8/6lt_81xn47d4n2k641z52rg00000gn/T/form-init8328218573408236617.clj:1:125)
    at clojure.lang.Compiler.load(Compiler.java:7391)
    at clojure.lang.Compiler.loadFile(Compiler.java:7317)
    at clojure.main$load_script.invokeStatic(main.clj:275)
    at clojure.main$init_opt.invokeStatic(main.clj:277)
    at clojure.main$init_opt.invoke(main.clj:277)
    at clojure.main$initialize.invokeStatic(main.clj:308)
    at clojure.main$null_opt.invokeStatic(main.clj:342)
    at clojure.main$null_opt.invoke(main.clj:339)
    at clojure.main$main.invokeStatic(main.clj:421)
    at clojure.main$main.doInvoke(main.clj:384)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at clojure.lang.Var.invoke(Var.java:383)
    at clojure.lang.AFn.applyToHelper(AFn.java:156)
    at clojure.lang.Var.applyTo(Var.java:700)
    at clojure.main.main(main.java:37)
Caused by: java.lang.NullPointerException
    at clojure.string$replace.invokeStatic(string.clj:101)
    at clojure.string$replace.invoke(string.clj:75)
    at redact.core$redact_doc.invokeStatic(core.clj:12)
    at redact.core$redact_doc.invoke(core.clj:7)
    at redact.core$_main.invokeStatic(core.clj:54)
    at redact.core$_main.doInvoke(core.clj:50)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at clojure.lang.Var.invoke(Var.java:383)
    at user$eval5.invokeStatic(form-init8328218573408236617.clj:1)
    at user$eval5.invoke(form-init8328218573408236617.clj:1)
    at clojure.lang.Compiler.eval(Compiler.java:6927)
    at clojure.lang.Compiler.eval(Compiler.java:6917)
    at clojure.lang.Compiler.load(Compiler.java:7379)
    ... 14 more

And here is my code:

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

(defn redact-doc
  ;; Reads the file line by line and redacts all the matched words
  ([target stoplist]
   (if (empty? stoplist)
     (str/trim target)
     (redact-doc (str/replace target (re-pattern (str "\\s(" (first stoplist) ")(\\s|$)")) " REDACTED ") (rest stoplist))))
  )

(defn get-target-text
  ;; Takes a vector of args and returns a String of a text file or and sentances
  ([args] (get-target-text args ""))
  ([args result]
   (if (empty? args)
     result
     (get-target-text (rest args) (if (boolean (re-find #"(.+\.[^csv\s])" (first args)))
                                    (str result (slurp (first args)))
                                    (if (not (boolean (re-find #"(.+\.csv|.+,.+)" (first args))))
                                      (if (boolean (re-find #"\s" (str/trim (first args))))
                                        (str result (first args) " ")))))))
 )

(defn read-csv
  ;; Takes in a filename and returns a vector of the csv values
  [file-name]
  (str/split (with-open [rdr (io/reader file-name)]
     (doall (reduce str (line-seq rdr)))) #","))

(defn gen-stoplist
  ;; Generates the stoplist for words to be redacted
  ([args] (gen-stoplist args []))
  ([args stoplist]
   (if (empty? args)
     stoplist
     (gen-stoplist (rest args) (if (boolean (re-find #"(.+\.csv)" (first args)))
                                 (into [] (concat stoplist (read-csv (first args))))
                                 (if (boolean (re-find #"(.+\..[^csv\s])" (first args)))
                                   stoplist
                                   (if (boolean (re-find #"(.*,.*)" (first args))) 
                                     (into [] (concat stoplist (str/split (first args) #",")))
                                     (if (boolean (re-find #"(\s)" (str/trim (first args))))
                                       stoplist
                                       (into [] (concat stoplist [(first args)] ))))))))))

(defn -main
  ([& args]
   (def stoplist (gen-stoplist args))
   (def target-text (get-target-text args))
   (println (redact-doc target-text stoplist)))
)

I have been staring at this trying to figure out what is causing the issue. I have tested all of the methods independently on the REPL and they all seem to work but the (-main) method is throwing a null pointer exception on the str/replace call....just not sure why. Any help you can give is much appreciated. Thanks!

1
  • target-text in your main method is nil. (get-target-text ["this is the other different thing" "this,different,other"]) returns nil. Commented Jul 30, 2016 at 6:05

1 Answer 1

1

There is a fair bit about your code which is not really correct. My guess is that our getting that call because your calling a function which is expecting a value and is getting a nil passed in - my guess would be one of the string functions.

your function definitions are not quite right. If your function only has a single 'signature' then you don't need the additional brackets. You should also use let bindings inside rather than def. e.g.

(defn -main
    [& args]
    (let [stoplist (gen-stoplist args)
          target-text (get-target-text args))]
      (println (redact-doc target-text stoplist)))

Your code is not passing what you think to gen-stoplist or get-target-text. I suspect the null pointer is because of the call to str/trim being passed a nil rather than a string.

My suggestion would be to open a repl and interact with it using some println in your functions to look at what is getting parsed in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.