NullPointerException when trying to read from webpage

I am building a page in luminus. The page has an input field with default values like this:

<input class="button is-light is-small" type="submit" value="Submit input">
	<input class="input" type="text" name="barea" value="897">
<input class="input" type="text" name="parea" value="345">

and this is used in a function:

(defn book [req]
  (def parea (read-string (get-in req [:params :parea])))
  (def barea (read-string (get-in req [:params :barea])))
  (layout/render req "book.html" {:res (* parea barea)}))

The problem is that I often get a nullpointerexeption when trying to read the varibles parea and barea. it seems that clojure evals the function before the default values are set in the html template. any ideas?

Are you sure req is of the shape you expect, that it has :params and it’s a map with keywords as keys?

yes, it works sometimes and sometimes not. params is the varraibles sent to the function via request. I suspekt that clojure evaluates the function before the default values have been set on the template.

If you’re setting the values as in your OP, via HTML and not via JS, then it’s not possible. You can easily check by inspecting the relevant request in the Network tab of your browser’s DevTools.

It’s not possible to tell what exactly is going on without having a proper minimal reproducible example.

I think the problem is that when the page is loaded no value is set and therefore a null pointer error. I found a temporary soloution by cheking the request and setting pre vallues in home.clj like this:

(def parea (Float/parseFloat (if (get-in req [:body-params :parea]) (get-in req [:body-params :parea]) "166")))

I know this is not what you asked about, but I highly recommend not using def within functions, and definitely don’t def the same variable in different functions. You are going to run into weird issues when multiple threads start accessing your variables.

Use let to define variables within functions.

thanks. good comment, I will change that

This is kinda hard to tell from just this bit of code but I’m guessing you are using the same “route” to get the initial form and posting the result to.

So how the Browser works is sending an initial request getting the HTML including the form. This request can’t know any of the form values since the Browser just has an empty page at this point. Then the <form> element is submitted via either <form method="get"> or <form method="post">. This second request will then contain the data from the form.

There are several ways to handle this but this is likely why you are occasionally get a NPE. If <form method="get"> is used the params will become part of the URL itself. So once they are there and you keep reloading they’ll be available. If you however visit the page without those params they’ll error out.

Propably right. But I do think that when the book function is called, nothing is at the form. I tried to counter this by including values in the form that could be used. But eventually, the if statement will sovle this temporarly.

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