component

Daouda 2019-06-14T02:42:44.036500Z

Hey Folks, I’ve created a component and just trying to eval it. But i keep getting NPE. Any clue about the root of this error? I am not passing any values, just want to eval the form.

seancorfield 2019-06-14T03:30:13.037Z

Hard to say without a lot more detail

Daouda 2019-06-14T03:33:11.037200Z

oh sorry

Daouda 2019-06-14T03:33:21.037600Z

let me copy and paste my code here

(ns patrimoine.components.database
  (:require [com.stuartsierra.component :as component]
            [clojure.tools.logging :as log]
            [monger.core :as monger]
            [monger.collection :as monger-coll]
            [monger.credentials :as monger-cred]
            [patrimoine.protocols.database :as protocols.database]))

(def ^:const host "host")
(def ^:const port "port")
(def ^:const pwd "pwd")
(def ^:const database-name "database-name")
(def ^:const username "username")

(defn- connect-to-database [host port username database-name pwd]
  (monger/connect-with-credentials host port (monger-cred/create username database-name pwd)))

(defrecord Database []
  component/Lifecycle

  (start [component]
    (log/info ";; Starting Database")
    (let [conn (connect-to-database host port  username database-name pwd)]
      (assoc component :connection conn)))

  (stop [component]
    (println ";; Stopping database")
    (monger/disconnect (:connection component))
    (assoc component :connection nil))

  protocols.database/IDatabase
  (insert-and-return! [component collection-name document]
    (try
      (monger-coll/insert-and-return (monger/get-db (:connection component)) collection-name document)
      (catch Exeception e
        (log/info e))))

  (find-maps! [component collection-name query]
    (try
      (monger-coll/find-maps (monger/get-db (:connection component)) collection-name query)
      (catch Exception e
        (log/info e))))

  (find-one-as-map! [component collection-name query]
    (try
      (monger-coll/find-one-as-map (monger/get-db (:connection component)) collection-name query)
      (catch Exception e
        (log/info e))))
  (find-map-by-id! [component collection-name id]
    (try
      (monger-coll/find-map-by-id (monger/get-db (:connection component)) collection-name id)
      (catch Exception e
        (log/info e))))

  (update-by-id! [component collection-name id query]
    (try
      (monger-coll/update-by-id (monger/get-db (:connection component)) collection-name id query)
      (catch Exception e
        (log/info e)))))

(defn new-database [] map->Database {})

seancorfield 2019-06-14T05:01:46.039Z

Your new-database function is missing ( .. ) around the call to map->Database {} forms.

seancorfield 2019-06-14T05:02:14.039600Z

(defn new-database [] (map->Database {}))
is what it should be @quieterkali

seancorfield 2019-06-14T05:03:15.040200Z

(although I wouldn't expect the protocol function calls to work at all, without that)

seancorfield 2019-06-14T05:03:55.040700Z

And, normally, sharing the stacktrace via a Gist or pastebin or something also helps people help you..

Daouda 2019-06-14T05:58:29.043400Z

Thank you very much @seancorfield 😄 . Your solution fixed the issue. Next time I will put it in gist.

seancorfield 2019-06-14T06:02:19.043800Z

Have you watched Stu Halloway's talk "Debugging with the Scientific Method"?

Daouda 2019-06-14T15:06:08.045200Z

No @seancorfield, but i going to watch it right now, thank you to mention it 🙂