Search code examples
c#xamarin.iosxamarinlinq-to-twitter

NullReferenceException using LinqToTwitter Search functionality


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)
                                 .SingleOrDefaultAsync();

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 by Joe Mayo himself on the 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?


Solution

  • For historical purposes, I owe you guys an answer to the possible solution.

    In short: manually editing the NuGet's packages.config to set a lowercase id for linqtotwitter seems to remove all my problems.

    The problem was never really able to solve itself. I am using v3.1.2 right now and have also experienced problems with the package that could not be restored (Note: only happens with Xamarin Studio on the Mac which is case-sensitive). But than I ran into this topic of users experiencing rather similar problems because of NuGet. I edited the packages.config manually (in both PCL and iOS project) to declare id="linqtotwitter" instead of "LinqToTwitter" which showed me the magic I was hoping for...

    Hope this helps...