Home > Enterprise >  Why im getting System.NullReferenceException: Object reference not set to an instance of an object
Why im getting System.NullReferenceException: Object reference not set to an instance of an object

Time:06-01

Can someone explain this exception to me? I have this method in User controller

[HttpGet("{username}/plays")]
public async Task<ActionResult<int>> GetUserPlays(string username)
{
    return Ok(await _unitOfWork.TrackRepository.CountPlays(username));
} 

And without going into the TrackRepository method, it throws an exception:

2022-05-31 14:37:02.8644|0|ERROR|gspark.Middleware.ExceptionMiddleware|Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object.
   at gspark.Controllers.UsersController.GetUserPlays(String username) in C:\Users\GSpark\source\repos\gspark\gspark\Controllers\UsersController.cs:line 119
   at lambda_method202(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)

TrackRepository method:

public async Task<int> CountPlays(string username)
{
    return await _context.Tracks
       .Include(t => t.User)
       .Where(t => t.User.UserName == username)
       .SumAsync(t => t.Plays);
}

Is it something about mapping? Or if it's Repository method causing a problem and if so why debug don't get into this method? Where and why this exception is thrown?

CodePudding user response:

If you don't reach CountPlays then that would suggest that either _unitOfWork or TrackRepository is null.

To clarify this, drop in a breakpoint and inspect the values in the debugger. Or, if you cannot do this then I would recommend an if statement similar to:

if (_unitOfWork == null || _unitOfWork.TrackRepository == null) 
{
    return new Exception("Ooops");
}

If you still have no joy, then share the code that initialises the _unitOfWork object

  • Related