C
C#10h ago
santiw01

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
No description
11 Replies
Angius
Angius10h ago
Check what exactly is null here I assume the return?
Jimmacle
Jimmacle10h ago
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
cap5lut
cap5lut10h ago
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 there
Jimmacle
Jimmacle9h ago
also fwiw, a NRE has nothing to do with the actual SQL query, that's purely related to your C# code
abhiroop43
abhiroop438h ago
is the _connectionString set correctly?
santiw01
santiw01OP8h ago
idk how to do that yes, i have others query and all return what it is expected
Jimmacle
Jimmacle8h ago
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
santiw01
santiw01OP8h ago
It says that one of the properties of the model im using return null here: Budget_Management.Models.IndexAccountViewModel.AccountType.get returned null
Jimmacle
Jimmacle8h ago
a stack trace looks like this
ConsoleApplication1.MyCustomException: some message .... ---> System.Exception: Oh noes!
at ConsoleApplication1.SomeObject.OtherMethod() in C:\ConsoleApplication1\SomeObject.cs:line 24
at ConsoleApplication1.SomeObject..ctor() in C:\ConsoleApplication1\SomeObject.cs:line 14
--- End of inner exception stack trace ---
at ConsoleApplication1.SomeObject..ctor() in C:\ConsoleApplication1\SomeObject.cs:line 18
at ConsoleApplication1.Program.DoSomething() in C:\ConsoleApplication1\Program.cs:line 23
at ConsoleApplication1.Program.Main(String[] args) in C:\ConsoleApplication1\Program.cs:line 13
ConsoleApplication1.MyCustomException: some message .... ---> System.Exception: Oh noes!
at ConsoleApplication1.SomeObject.OtherMethod() in C:\ConsoleApplication1\SomeObject.cs:line 24
at ConsoleApplication1.SomeObject..ctor() in C:\ConsoleApplication1\SomeObject.cs:line 14
--- End of inner exception stack trace ---
at ConsoleApplication1.SomeObject..ctor() in C:\ConsoleApplication1\SomeObject.cs:line 18
at ConsoleApplication1.Program.DoSomething() in C:\ConsoleApplication1\Program.cs:line 23
at ConsoleApplication1.Program.Main(String[] args) in C:\ConsoleApplication1\Program.cs:line 13
santiw01
santiw01OP7h ago
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 guess
Jimmacle
Jimmacle6h ago
that 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

Did you find this page helpful?