title: JAVA: Possible to add a runnable thread into a queue?
body: <p>I recently began working with Threads and I am trying to complete a Java implementation of the Looper class in Android.  Basically I am making a Java class that puts threads into a queue that will then be executed by the Looper class.  I have the code completed for the most part but have an issue with the enqueuing of tasks.  </p>
  
  <p>In the Looper class I have the queue declared and my enqueue method:</p>
  
  <pre><code>List&lt;Runnable&gt; queue;   
  
  public synchronized void enqueue(Runnable runnable) {
      queue.add(runnable);
      notify(); // signal a waiting thread
  }
  </code></pre>
  
  <p>I then created another class called TaskManager to add Tasks into the queue. I receive the error when I call:</p>
  
  <pre><code>loop.enqueue(new Task());
  </code></pre>
  
  <p>Where Task() implements runnable and just adds two integers together in its run() method...this is just a test.</p>
  
  <p>The error I receive is:</p>
  
  <pre><code>Exception in thread "Thread-0" java.lang.NullPointerException
  at Looper.enqueue(Looper.java:20) (this is the queue.add(runnable))
  at TaskMaker.run(TaskMaker.java:16) (this is the loop.enqueue(new Task())
  </code></pre>
  
  <p>I'm obviously doing something wrong and not implementing this right...how should I go about this? Is the way I am enqueuing the task right? Thanks for any help it is much appreciated!</p>