jwesonga
jwesonga

Reputation: 4383

Clojure and Compojure: Response Map is nil

I'm using compojure for a basic web app, I have this code in core.clj:

(defroutes routes
      (GET "/" [] (layout/application "Home" (contents/index)))
      (route/resources "/"))

(def application (handler/site routes))

(defn -main []
  (let [port (Integer/parseInt (or (System/getenv "PORT") "8090"))]
    (jetty/run-jetty application {:port port :join? false})))

When I access the 0.0.0.0:8090 everything is loading ok, but I keep seeing this error:

java.lang.NullPointerException: Response map is nil
    at ring.util.servlet$update_servlet_response.invokeStatic(servlet.clj:100)
    at ring.util.servlet$update_servlet_response.invoke(servlet.clj:91)
    at ring.util.servlet$update_servlet_response.invokeStatic(servlet.clj:95)
    at ring.util.servlet$update_servlet_response.invoke(servlet.clj:91)
    at ring.adapter.jetty$proxy_handler$fn__337.invoke(jetty.clj:27)
    at ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a.handle(Unknown Source)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:503)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
    at java.base/java.lang.Thread.run(Thread.java:844)

Any idea what's going on?

Upvotes: 0

Views: 847

Answers (1)

Alan Thompson
Alan Thompson

Reputation: 29958

@Svante is almost certainly correct. An easy way to verify is to use the spyx function from the Tupelo library:

(ns demo.core
  (:use tupelo.core))

(defroutes routes
  (GET "/" [] (spyx (layout/application "Home" (contents/index))))
  (route/resources "/"))

which will print something like:

(layout/application "Home" (contents/index))) => nil 

when run. spyx ("spy explicit") prints the expression you give it, an arrow, and the expression value. spy, spyx, spy-pretty, etc also return the value printed (unlike println which always returns nil) so you can insert a spy printout anywhere without disrupting the processing chain. Thus, you don't need to write something like:

(defroutes routes
  (GET "/" [] (let [tmp-1 (layout/application "Home" (contents/index))]
                (println "layout/application result => " tmp-1)
                tmp-1)))
  (route/resources "/"))

in order to get a debug message printed. In order to spy & friends, add this to the :dependencies in your project.clj:

[tupelo "0.9.138"]

Update

Hmmmm.... Not sure what could be the problem. I made a simple demo app from lein new compojure demo-compojure with the following:

(ns demo-compojure.handler
  (:use tupelo.core)
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [ring.middleware.defaults :refer [wrap-defaults site-defaults]]))

(defn index []
  (spy :index--result "Hello World"))

(defroutes app-routes
  (GET "/" [] (spyx (index)))
  (route/not-found "Not Found"))

(def app
  (wrap-defaults app-routes site-defaults))

and results:

~/expr/demo-compojure > lein ring server
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Started server on port 3000
:index--result => "Hello World"
(index) => "Hello World"

So that is working. Also, int? is a basic Clojure function, so that is puzzling. Perhaps make a clean demo project like the above and take it from there?

Upvotes: 1

Related Questions