'NULL exception error using Select method in MS Graph .NET SDK

I'm trying to use the Microsoft Graph .NET SDK Library to query the Microsoft Graph API. I've had no trouble making some basic requests, but now I'm trying to make some slightly more sophisticated requests I'm running into problems, exacerbated by the patchy and contradictory documentation. I'll probably create a few different questions to keep the issues separate. Here's the first one!

This request works just fine:

var response = await graphServiceClient
    .Users[myUpn]
    .Events
    .Request()
    .GetAsync();

I get a collection of the user's calendar events. Perfect. But now I only want a subset of the properties for each event, so I try to use the Select() method, per the documentation on GitHub:

var response = await graphServiceClient
    .Users[myUpn]
    .Events
    .Request()
    .Select("subject")
    .GetAsync();

This throws a System.ArgumentNullException error: "Value cannot be null.\r\nParameter name: source". I've also tried the syntax described in Microsoft's OTHER documentation:

var response = await graphServiceClient
    .Users[UserPrincipalName(username)]
    .Events
    .Request()
    .Select(e => new
    {
        e.Subject
    })
    .GetAsync();

This produces the same error. I have no idea which of these two forms should work (maybe both?), but neither does so it's a moot point. However, using the Graph Explorer I am able to make a successful request like this:

https://graph.microsoft.com/v1.0/users/{redactedUPN}/events?$select=subject

This suggests to me that what I'm attempting to do in the C# code should work. Anyone know what, if anything, I'm doing wrong here? And which of the two forms for the parameter in the Subject() method is correct?

Update

As requested, here is the full error response I get when calling my web service, written using the .NET SDK, including the stack trace. Apologies if this isn't formatted as well as it might be, I wasn't sure what the correct markdown was.

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Value cannot be null.\r\nParameter name: source",
    "ExceptionType": "System.ArgumentNullException",
    "StackTrace": "   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)\r\n   at MSGraphService.API.Controllers.v1_0_CalendarController.<>c.<TransformResult>b__6_0(Event e)\r\n   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at MSGraphService.API.Controllers.v1_0_CalendarController.TransformResult(ICollectionPage`1 events)\r\n   at MSGraphService.API.Controllers.v1_0_CalendarController.<UserEvents>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__1`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__6.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__6.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"
}


Solution 1:[1]

This is expected error , because its mentioned in the docs that All properties other than Id will be null on the returned user object. could you please try search by id , and see it stills it return you the same error , next step is to check is there any null value in any subject , this should be check by debugging the code.enter image description here.

you can try by your own custom query which will Handel the null exception ,please follow the above docs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vicky kumar