title: "Object reference not set to an instance of an object" when running on another thread
body: <p>I'm trying to use TweetSharp from a static class (called <code>TitterHandler</code>) so theoretically I can swap it out later.</p>
  
  <p>The example suggests using <code>Dispatch.BeginInvoke</code> so when the UI is updated it is done using the UI thread.</p>
  
  <p>I would like to just load data into the model, although admittedly this will trigger an <code>OnPropertyChanged</code> event which will update the UI. Since this is from a static method I use <code>System.Windows.Deployment.Current.Dispatcher.BeginInvoke</code>.</p>
  
  <p>However it keeps crashing with an 'Object not set to a reference ' exception. I have fairly limited understanding of threads but from what I gather </p>
  
  <ul>
  <li>The UI thread needs to update the UI but the main thread may fire an event which is then picked up by the UI thread - to test this I assign to a static variable totally separate from the UI (<code>TweetCache</code>) but the app still crashes</li>
  <li>The thread may be garbage collected if there are no references to it. I placed a reference (<code>Thread</code>) on the class but still it crashes.</li>
  </ul>
  
  <p>I am out of ideas, here is the code ...</p>
  
  <pre><code>public class TwitterHandler
  {
  
      public static TwitterService Service = new TwitterService(AppConfig.TwitterConsumerKey, AppConfig.TwitterConsumerSecret);
  
      internal static void LoadTweetsFromWeb()
      {
          TwitterHandler.Service.AuthenticateWith(AppConfig.TwitterToken, AppConfig.TwitterTokenSecret);
          TwitterHandler.Service.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions() 
              { ScreenName = AppConfig.NewsBrandingTwitterHandle, }, 
              TwitterHandler.OnTweetsLoaded);
      }
  
  
      // Put this here to maintain a refrence to the thread to avoid GC
      public static DispatcherOperation Thread;
  
      // A 'thread external' reference with no onward events that trigger UI changes
      public static List&lt;Tweet&gt; TweetCache;
  
      public static void OnTweetsLoaded(IEnumerable&lt;TwitterStatus&gt; statuses, TwitterResponse res)
      {
          if (res.StatusCode == HttpStatusCode.OK)
          {
              TwitterHandler.Thread = System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
              {
                  List&lt;Tweet&gt; tweets = new List&lt;Tweet&gt;();
                  foreach (TwitterStatus status in statuses)
                  {
                      tweets.Add(new Tweet()
                      {
                          Content = status.Text,
                          UrlString = status.Place.Url,
                      });
                  }
                  if (tweets.Count &gt; 0)
                  {
                      // App.Tweets = tweets;
                      // TweetCache raises no 'PropertyChanged' event
                      TwitterHandler.TweetCache = tweets;
                  }
  
              });
          }
  
  
      }
  
  
  
  
  }
  </code></pre>