This project has moved and is read-only. For the latest updates, please go here.

NullReferenceException in QueryTwitter for Windows Phone 7.1

Dec 17, 2011 at 11:57 AM

Hello Joe,

I used NuGet to add a reference to a WP7 Mango project in VS2010. The following code (and various variations on it) would always cause a NullReference exception to be thrown by Linq to Twitter shortly after the method ends.

(p.Items is an observable collection)

            TwitterContext twitterCtx = new TwitterContext(authorizer, "", "");

            var friendTweets =
                from tweet in twitterCtx.Status
                where tweet.Type == StatusType.Home &&
                      tweet.Page == 2
                select tweet;

            foreach (var tweet in friendTweets)
                p.Items.Add(new TweetViewModel() { AccountName = tweet.ScreenName, RealName = tweet.UserID, TweetBody = tweet.Text });

(end of method)

I downloaded and referenced the source code for change set 94498 and found that the code below throws the exception. Note that responseObject is correctly populated with 20 Status items.

In TwitterExecute.cs (with SILVERLIGHT and WINDOWS_PHONE symbols defined for the project) in the following method:

public string QueryTwitter<T>(Request request, IRequestProcessor<T> reqProc)
 AsyncCallback is always null.

                    new AsyncCallback(
                        ar =>
                            lock (this.asyncCallbackLock)
                                var res = reqEx.EndGetResponse(ar) as HttpWebResponse;
                                //httpStatus = res.Headers["Status"];
                                response = GetTwitterResponse(res);
                                List<T> responseObj = reqProc.ProcessResults(response);
                                (AsyncCallback as Action<IEnumerable<T>>)(responseObj); 
                        }), null);

Any thoughts on what might be the underlying cause of my issue?

Thanks in advance.


Dec 17, 2011 at 10:24 PM

Hi John,

In Silverlight and WP, you need to use the Async flavor of LinqToTwitter queries.  The example you used above is using a synchronous query.  Here's a snippet from the WindowsPhoneDemo project in the downloadable code that shows how to do this:

            (from tweet in ctx.Status
             where tweet.Type == StatusType.Public
             select tweet)
            .AsyncCallback(tweets =>
                Dispatcher.BeginInvoke(() =>
                    var publicTweets =
                        (from tweet in tweets
                         select new PublicTweet
                             UserName = tweet.User.Identifier.ScreenName,
                             Message = tweet.Text,
                             ImageSource = tweet.User.ProfileImageUrl

                    PublicTweetListBox.ItemsSource = publicTweets;

Notice that instead of a return value, the AsyncCallback handles the query results.  Likewise, for side-effect methods, like Update, you would use the overloads that accept callbacks.



Dec 18, 2011 at 2:01 PM

Hi Joe, thanks very much for your explanation (and patience). Moving creakily from the synchronous by default world of Windows forms programming I really need to shift my programming habits (and to remember to read the 'manual'!) .

Thanks again