NullReferenceException when api return null result.
See original GitHub issueTo Reproduce
Steps to reproduce the behavior:
- create an asp.net api project with version 2.2
- add code
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[Route("test")]
public Task<string> Testapi()
{
return null;
}
}
- test the api with browser
- it returns error
NullReferenceException: Object reference not set to an instance of an object.
lambda_method(Closure , object )
Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.GetAwaiter()
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Expected behavior
NullReferenceException should be handled propertly and return some useful information.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
c# - What is a NullReferenceException, and how do I fix it?
This means the reference is null , and you cannot access members (such as methods) through a null reference. The simplest case: string...
Read more >What is NullReferenceException in C#? - Code Maze
In C#, a NullReferenceException occurs when we try to access a variable whose value has not been set or has been set to...
Read more >Object Reference Not Set to an Instance of an Object
This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member—for...
Read more >Null Reference Exceptions
Null Reference Exceptions. A NullReferenceException happens when you try to access a reference variable that isn't referencing any object.
Read more >c# - What's wrong with returning null?
No, you are sometimes returning a null value. Either your signature doesn't say what you want it to, or you should throw.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I suspect that what’s happening here is the action is throwing and your error handler is what’s returning a 404.
So looks like I referred to something which we never did. So I’m removing my statement above regarding 3.0 to avoid spreading wrong information out. I had a chat with @rynowak regarding this and what is most probably happening in your case is you’re hitting the same exception as you do in 2.2, but… (and @rynowak just responded 😃 )