Synchronization on collection from anonymous inner class thread throws null pointer exception
<p>Android JVM throws a null pointer exception when my inner class tries to "synchronized" on a collection contained in the base class.  If I removed the call to synchronized, the code works fine.  Of course, then concurrency will kick my ass later so I need to find a solution to this.  </p>

<p>I'm new to android but not new to Java.  This implementation of the observer pattern has always worked for me in Java.  If I have to explicitly pass my base class into a non anonymous class to do it I will but what a pain in the ass.  I could also create a new update function and call synchronized from inside the base class but that requirement seems to violate the access rules that should be allowed in an anonymous class.</p>

<pre><code>public class BaseClass
{
  private ArrayList&lt;IListenerInterface&gt; myListeners = new ArrayList&lt;IListenerInterface&gt;();

  public BaseClass()
  {
    StartWorker();
  }


  public void addListener(IListenerInterface listener)
  {
    synchronized(myListeners)
    {
        myListeners.add(listener);
    }
  }

  public void StartWorker()
  {
    Thread myThread = new Thread(new Runnable() {
      public void run() {
        while(true)
        {
          // ... blocks here until work to be done
          // then...do some work ....

          // now try to update the listeners
          synchronized(myListeners)    // &lt;-- NULL POINTER EXCEPTION Call back to the enclosing classes myListeners collecion
          {
            // never get to this point...
            myListeners.Update(someWork);
          }
        }
      }
    });


  }
}
</code></pre>
