title: How to dereference Future in Clojure?
body: <p>My program works as following: two futures transfer a specific amount from one balance A to balance B after some random wait-time. Each repeats doing that 10 times. So I try to stop the program and deref the future, but it doesnot work, it throws NullPointerException as I deref it. What have I done wrong?</p>
  
  <p>And I want a final result that displays balanceA and balanceB at the last line before the program ends. Right now it does outputs the result but it appears inside the "transfer" and "try". What do I have to do?</p>
  
  <p>Thank you.</p>
  
  <pre><code>  (def balanceA (ref 1000))
    (def balanceB (ref 2000))
    (def agentCount (agent 1))
  
     ;;transfer function
    (defn transfer [balanceA balanceB amount futureNum waitingTime]
      (dosync(alter balanceA - amount))
      (Thread/sleep waitingTime)
      (dosync(alter balanceB + amount))
      (do(
          (println "Trying" futureNum waitingTime)
          (println "Transfered" futureNum @agentCount)
          (send-off agentCount inc))))
  
    ;;futureA 
    (dotimes [n 10](def futureA (future (transfer balanceA balanceB 20 1 (rand-int 100)))))
  
    ;;futureB
    (dotimes [n 10](def futureB (future (transfer balanceA balanceB 15 2 (rand-int 40))(prn "result" @balanceB @balanceB))))
  
    ;;print out the result
    (println "result" @balanceA @balanceB)
  
    ;;dereference futures
    (@futureA)
    (@futureB
  </code></pre>
  
  <p>Here is my output:</p>
  
  <pre><code>  user=&gt; (load-file "assignment10.clj")
    Result: Trying: Trying: 2650  2000
    2 Trying: 2 Trying: 2 3
     Transfered: 2 1
  
    user=&gt; Trying: 1 28
    Transfered: 1 2
    Trying: 1 28
     Transfered: 1 3
    Trying:Trying:  21  2218
  
    Transfered: 2 4
    Transfered: 1 4
   97
  
    Transfered: 2 6
    10
     Trying: 2 26
    Transfered: 2 7
    Transfered: 2 Trying: 8
    2Trying:Transfered:   227
    2 28
    Transfered:7
    Transfered: 2 9
    2 9
    Trying: 2 33
    Transfered: 2 12
    Trying: 1 49
     Trying: 2 39
    Transfered:Transfered:  21  1313
  
    Trying: 1 57      
    Transfered: 1 15
    Trying: 1 71
    Transfered: 1 16
    Trying: 1 76
    Transfered: 1 17
    Trying: 1 81
    Transfered: 1 18
    Trying: 1 93
    Transfered: 1 19
    Trying: 1 98
    Transfered: 1 20
  </code></pre>