NullReferenceException on basic Search functionality

Jan 19, 2015 at 6:40 AM
Same question as posted on StackOverflow some days ago:

I'm using LinqToTwitter v3.1.1 from Nuget with Application-Only Authentication in my Xamarin.IOS Unified App to display a list of Tweets from a TwitterAccount using the Search option of this nice library. However, when executing the simple search, I get an unhandled NullReference Exception on the TwitterQueryProvider.

The code I have written is as follows:
var credentialStore = new InMemoryCredentialStore
    ConsumerKey = "MyTwitterKey",
    ConsumerSecret = "MyTwitterSecret"

var authorizer = new ApplicationOnlyAuthorizer { CredentialStore = credentialStore };
await authorizer.AuthorizeAsync();

var twitterCtx = new TwitterContext(authorizer);

var searchResponse = await (from search in twitterCtx.Search
                           where search.Type == SearchType.Search 
                              && search.Query == "Microsoft"
                          select search)

if (searchResponse != null && searchResponse.Statuses != null)
    foreach (var tweet in searchResponse.Statuses)
        Debug.WriteLine("User: {0}, Tweet: {1}", tweet.User.ScreenNameResponse, tweet.Text);
Part of the stacktrace below:
{System.NullReferenceException: Object reference not set to an instance of an object 
 at LinqToTwitter.TwitterQueryProvider+<ExecuteAsync>d__6`1[System.Collections.Generic.IEnumerable`1[LinqToTwitter.Search]].MoveNext () [0x00000] in <filename unknown>:0
 --- End of stack trace from previous location where exception was thrown --- 

 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in [somepath]/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Object].GetResult () [0x00034] in [somepath]/System.Runtime.CompilerServices/ConfiguredTaskAwaitable_T.cs:62 
 at LinqToTwitter.TwitterExtensions+<ToListAsync>d__11`1[LinqToTwitter.Search].MoveNext () [0x00000] in <filename unknown>:0 
 --- End of stack trace from previous location where exception was thrown --- 

 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in [somepath]/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
 at System.Runtime.CompilerServices.TaskAwaiter`1[System.Collections.Generic.List`1[LinqToTwitter.Search]].GetResult () [0x00034] in [somepath]/System.Runtime.CompilerServices/TaskAwaiter_T.cs:59 
 at [somepath].TwitterService+<GetTweets>d__2.MoveNext () [0x0014f] in [somepath]\TwitterService.cs:27 }
The errors occur when the Search is executed and put into the var searchResponse. I have validated that the authentication has succeeded and a bearertoken has been set. No fancy stuff has been done other than the simple example provided on this LINQ to Twitter Project site on Codeplex.

I have also tried something without using LinqToTwitter which worked (with the same credentials) up to receiving the list of tweets, but due to serialization reasons I've chosen to go for LinqToTwitter.

It feels like I'm missing something obvious here like setting some token or authorizing somewhere, but cannot find it. The demo console project included with the source files from codeplex seems to work perfectly fine. Any ideas here?
Jan 20, 2015 at 4:34 PM

I've been unable to reproduce this. However, I just committed a new demo project named Linq2TwitterDemos_XForms which is available in the downloadable source code. Please try to run that. If it works, it might help you find the delta between your code and the demo. I don't see anything wrong with your code.

Jan 21, 2015 at 1:15 PM
Hi Joe, Thanks for your reply,

I have been progressing a little on this topic. I took this bunch of code out of my Xamarin.iOS project into a Console App to run it out of my app. It seems to work perfectly fine there. At that point I started checking my references, where I saw that only my Core project was referencing the Nuget Package. Have encountered something like this before, because Xamarin.iOS seems to have a need to be aware of this library too. Have tried to add it over there, but some Microsoft.Bcl packages are then complaining about not being able to be added to this Xamarin.IOS v1.0 version. Am looking at your blogpost to see how you came around this.

Seems for now that the above has been satisfied and the issue has been found (yet not to be solved, will return with it when I find it)...