Object reference not set to an instance of an object when Invoking generic reflective method

So this problem is quite confusing. And its fairly complicated and abstract so I'm going to try my best to explain it.

I have 3 classes LinkedIn, Facebook, and Twitter. All of them inherit from a common base class SocialBase which only has one property: uuid and this property is only used so we can find the actual types in the database.

So I have this one function that accepts a Profile class which contains foreign keys to all LinkedIn, Facebook, and Twitter tables, as well as an Enum value that will tell us whether to look for the LinkedIn, the Facebook, or the Twitter foreign keys

public static async Task UnlinkSocialAccountFromProfile(Profile prof, SocialNetworks provider)
        //TYPE variable of LinkedIn, Facebook, or Twitter
        var handler = HandlerMapping[provider];
        //client is MobileServiceClient
        var method = client.GetType().GetMethod("GetTable", Type.EmptyTypes);
        var generic = method.MakeGenericMethod(handler);
        var table = generic.Invoke(client, null);
        //Profile has 3 foreign keys, LinkedinUUID, FacebookUUID, TwitterUUID, we want the <handler>UUID
        string propertyValue = prof.GetType().GetProperty(handler.Name + "UUID").GetValue(prof) as string;
        //Invoke Extension method with our generic types
        var genMethod =
            typeof (Extensions).GetMethod("FilterByNamedProperty")
                .MakeGenericMethod(table.GetType().GetGenericArguments()[0], propertyValue.GetType());
        //Get the List<handler> that results from our query
        var result = await (Task<List<Linkedin>>)(genMethod.Invoke(null, new [] {table, "uuid", propertyValue}));
        //var result = await (table as IMobileServiceTable<SocialBase>).FilterByNamedProperty("uuid", propertyValue);
        await new SocialResources().DeleteIfExists((result as IList<SocialBase>)[0]);

So what I'm doing here is getting the Type of the class, so this will be one of the SocialBase subclasses. For this particular example I know I'm going to be looking for the LinkedIn type. So after getting type LinkedIn I need to invoke the generic method from my MobileServiceTable so normally it would look like MobileServiceTable.GetTable<LinkedIn>() but thanks to reflection we have to take the longer route around this.

After getting my returned instance of IMobileServiceTable<LinkedIn> I get value of the foreign key I'm looking for. In this case it will be called LinkedInUUID. Now here comes the tricky part. I have this extension method that will construct my query expression for me since it has to be of type Expression<Func<LinkedIn, bool>>

public async static Task<List<TSource>> FilterByNamedProperty<TSource, TValue>(this IMobileServiceTable<TSource> source, string propertyName, TValue value)
        // uuid
        var property = typeof(TSource).GetProperty(propertyName);
        // (TSource)p
        var parExp = Expression.Parameter(typeof(TSource));
        var methodExp = Expression.Property(parExp, property);
        // value
        var constExp = Expression.Constant(value, typeof(TValue));
        // p.uuid == value
        var binExp = Expression.Equal(methodExp, constExp);
        // p => p.uuid == value
        var lambda = Expression.Lambda<Func<TSource, bool>>(binExp, parExp);
        return await source.Where(lambda).ToListAsync(); 

I believe the comments do a pretty good job of explaining the build process as each statement occurs. However, once we get to the return await ... the application will crash. Here is the output right before that line and the error immediately following.

IMobileServiceTable<TSource> source = {Microsoft.WindowsAzure.MobileServices.MobileServiceTable<SocialConnect.Linkedin>} (Correct)

propertyName = uuid (Correct)

TValue value = dscRJQSIxJaEfd (Correct)

I have a strong feeling the problem is with my lambda expression, but if I test the extension with this line var test = await (source as IMobileServiceTable<Linkedin>).Where(p => p.uuid == (value as string)).ToListAsync(); it works perfectly fine. But as soon as I change it to use the lambda variable than I get the exception. The actual value of the lambda expression is {Param_0 => (Param_0.uuid == "dscRJQSIxJaEfd")} which looks correct

Any ideas?

EDIT Sorry, here is the actual exception from stack trace

`A first chance exception of type 'System.NullReferenceException' occurred in mscorlib.dll
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.GetTableMemberName(Expression expression, MobileServiceContractResolver contractResolver)
   at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.VisitMemberAccess(MemberExpression expression)
   at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.Visit(Expression node)
   at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.VisitBinary(BinaryExpression expression)
   at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.Visit(Expression node)
   at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.Compile(Expression expression, MobileServiceContractResolver contractResolver)
   at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryTranslator1.AddFilter(MethodCallExpression expression)
   at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryTranslator1.VisitMethodCall(MethodCallExpression expression)
   at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryTranslator1.Visit(Expression expression)
   at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryTranslator1.Translate()
   at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryProvider.Compile[T](IMobileServiceTableQuery1 query)
   at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryProvider.<Execute>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
   at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQuery1.<ToListAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
   at SocialConnect.Models.Extensions.<FilterByNamedProperty>d__22.MoveNext() in MobileServiceSample\MobileServiceSample\Extensions.cs:line 55`


Alright I finally solved this issue. Apparently the problem wasn't with the query or anything I was doing, but with the SocialBase class. I had been using it to make methods where I knew the generic type TEntity would have a uuid property so I could easily find it in any database. But I guess the field was not getting serialized properly and that was what was causing the NullReferenceException. After removing the base class and just reflectively finding the property and value then everything starting working fine.

I don't understand why it wasn't working properly before because I had been using the base SocialBase class for a lot of situations before this.

Need Your Help

Displaying jsGrid nested object array

arrays object nested jsgrid

Using jsGrid I am attempting to display information from my server. The format that I receive the data is thus:

How to get metadata from a picture?

android bitmap metadata

I am browsing the web but no way to find how to get metadata from a picture.