clojurerendernoir

Noir render function causes NullPointerException


In my Clojure application I'm using Noir as web framework. I have one post page defined which is saving some data onto a mongodb database and I'm then trying to render another page:

(defpage "/delivery/save" [delivery]
 (let [id (save-delivery delivery)]
    (render "/delivery/list" id))

I then have another page:

(defpage "/delivery/list/:id" {:id id}
   (this should render the page))

However I'm getting a NullPointerException when calling the render function. Any idea why is this happening? Please let me know if you need more details.

Thanks, Nico

Here's the stacktrace:

java.lang.NullPointerException
    at noir.core$render.doInvoke(core.clj:161)
    at clojure.lang.RestFn.invoke(RestFn.java:423)
    at mailjure.views.delivery$POST__delivery__save.invoke(delivery.clj:115)
    at mailjure.views.delivery$eval477$fn__478.invoke(delivery.clj:109)
    at compojure.core$if_route$fn__271.invoke(core.clj:39)
    at compojure.core$if_method$fn__264.invoke(core.clj:24)
    at compojure.core$routing$fn__286.invoke(core.clj:98)
    at clojure.core$some.invoke(core.clj:2390)
    at compojure.core$routing.doInvoke(core.clj:98)
    at clojure.lang.RestFn.applyTo(RestFn.java:139)
    at clojure.core$apply.invoke(core.clj:603)
    at compojure.core$routes$fn__290.invoke(core.clj:103)
    at noir.server.handler$init_routes$fn__1422.invoke(handler.clj:92)
    at noir.request$wrap_request_map$fn__1324.invoke(request.clj:14)
    at hiccup.middleware$wrap_base_url$fn__860.invoke(middleware.clj:12)
    at noir.session$noir_flash$fn__1347.invoke(session.clj:92)
    at ring.middleware.flash$wrap_flash$fn__642.invoke(flash.clj:14)
    at noir.session$noir_session$fn__1337.invoke(session.clj:56)
    at ring.middleware.session$wrap_session$fn__635.invoke(session.clj:40)
    at ring.middleware.cookies$wrap_cookies$fn__572.invoke(cookies.clj:150)
    at noir.cookies$noir_cookies$fn__1188.invoke(cookies.clj:66)
    at ring.middleware.cookies$wrap_cookies$fn__572.invoke(cookies.clj:150)
    at noir.validation$wrap_noir_validation$fn__1375.invoke(validation.clj:90)
    at noir.statuses$wrap_status_pages$fn__1286.invoke(statuses.clj:34)
    at ring.middleware.reload$wrap_reload$fn__712.invoke(reload.clj:18)
    at noir.exception$wrap_exceptions$fn__1317.invoke(exception.clj:57)
    at noir.options$wrap_options$fn__1277.invoke(options.clj:34)
    at compojure.core$routing$fn__286.invoke(core.clj:98)
    at clojure.core$some.invoke(core.clj:2390)
    at compojure.core$routing.doInvoke(core.clj:98)
    at clojure.lang.RestFn.applyTo(RestFn.java:139)
    at clojure.core$apply.invoke(core.clj:603)
    at compojure.core$routes$fn__290.invoke(core.clj:103)
    at ring.middleware.keyword_params$wrap_keyword_params$fn__445.invoke(keyword_params.clj:27)
    at ring.middleware.nested_params$wrap_nested_params$fn__482.invoke(nested_params.clj:65)
    at ring.middleware.params$wrap_params$fn__390.invoke(params.clj:76)
    at ring.middleware.multipart_params$wrap_multipart_params$fn__417.invoke(multipart_params.clj:103)
    at ring.adapter.jetty$proxy_handler$fn__377.invoke(jetty.clj:16)
    at ring.adapter.jetty.proxy$org.mortbay.jetty.handler.AbstractHandler$0.handle(Unknown Source)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

Solution

  • You need to specify exactly the same url:

     (defpage "/delivery/list/:id" {id :id}
       "whoot!")
    
     (defpage "/delivery/save" []
       (let [id 100]
         (render "/delivery/list/:id" id)))
    

    But this is ugly, it is better to give names to your pages:

     (defpage delivery-page "/delivery/list/:id" {id :id}
       "whoot!")
    
     (defpage "/delivery/save" []
       (let [id 100]
         (render delivery-page id)))
    

    Which is pretty neat. Also it has the advantage that you can easily test your delivery-page:

     (delivery-page 123)