java.lang.NumberFormatException null

I am catching a number from path-params in my luminus project (the variable hyra) with the following code:

(defn btable2 [{:keys [params path-params]}]
  (let [spara (:spara path-params)
        hyra (Long/parseLong (:hyra params))
        tarea (Long/parseLong (:tarea params))
        omk (Long/parseLong (:omk params))
        barea (Long/parseLong (:barea params))
        parea (Long/parseLong (:parea params))
        tarea (Long/parseLong (:tarea params))
        notes (:notes params)
        totk (/ (/ (* (+ (/ hyra tarea) (/ omk tarea)) (+ parea barea) 15.0) 10.0))
        totk2 (Math/round (* 100000 totk))
        latest {:hyra hyra :tarea tarea :omk omk :barea barea :notes notes :totk totk :totk2 totk2}]
    (if (= 1 spara)
      (db/addb! latest)
      (layout/render params "btable.html"  {:latest (assoc params :notes notes :totk totk :totk2 (Math/round (* 100000 totk)))}))))

but when trying to add the values to my database

(db/addb! latest)

I get the above error. I thought Long/parseLong would take care of the conversion? any ideas?

It looks like one of those params is not coming through as expected – a nil rather than a String.

You shouldn’t expect Long/parseLong to handle conversion of nil: (java.lang.Long/parseLong nil) throws java.lang.NumberFormatException in my REPL, which matches the javadoc, which says it accepts a String parameter and “throws NumberFormatException if the string does not contain a parsable long.”

I think I solved it, it seems that one of the buttons was outside of the form tag, so I included it in the original form tag:

  <form id="beräkna2">
  <button class="button is-small is-link is-light" formaction="/btable2/0">beräkna</button>
  </form>
  <form id="beräkna2">
  <button class="button is-small is-link is-light" formaction="/btable2/0">beräkna</button>
  <button class="button is-small is-link is-light" formaction="/btable2/1">spara</button>
  </form>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.