Fork me on GitHub
#component
<
2019-06-14
>
Daouda02:06:44

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.

seancorfield03:06:13

Hard to say without a lot more detail

Daouda03:06:21

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 {})

seancorfield05:06:46

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

seancorfield05:06:14

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

seancorfield05:06:15

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

seancorfield05:06:55

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

Daouda05:06:29

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

seancorfield06:06:19

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

Daouda15:06:08

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