Object reference not set to an instance of an object.
This error appears when I run the query shown in the picture. Even when I hardcode the userId, the same error persists. However, when I run the same query in SQL Server, it returns the expected result

11 Replies
Check what exactly is
null
here
I assume the return?also share the stack trace so we know if the exception is actually thrown by this method or if it's elsewhere in the call stack
it has to be somewhere else,
the only member access (which would cause a NRE) is
connection.QueryAsync...
,
and its impossible that connection
is null therealso fwiw, a NRE has nothing to do with the actual SQL query, that's purely related to your C# code
is the _connectionString set correctly?
idk how to do that
yes, i have others query and all return what it is expected
you should be able to see the stack trace in the debugger when you get the exception
unless you're catching it and throwing data away before you display it somewhere
It says that one of the properties of the model im using return null here: Budget_Management.Models.IndexAccountViewModel.AccountType.get returned null
a stack trace looks like this
System.NullReferenceException: Object reference not set to an instance of an object.
at AspNetCoreGeneratedDocument.Views_Account_Index.ExecuteAsync() in C:\@PROGRAMACION\@DOTNET\Budget Management\Views\Account\Index.cshtml:line 60
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable1 statusCode)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable1 statusCode)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable1 statusCode)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Here, i guessthat doesn't mention the code you screenshotted at all
it says the NRE is being thrown at
C:\@PROGRAMACION\@DOTNET\Budget Management\Views\Account\Index.cshtml:line 60