C
C#4mo 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
13 Replies
Angius
Angius4mo ago
Check what exactly is null here I assume the return?
Jimmacle
Jimmacle4mo 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
cap5lut4mo 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
Jimmacle4mo ago
also fwiw, a NRE has nothing to do with the actual SQL query, that's purely related to your C# code
abhiroop43
abhiroop434mo ago
is the _connectionString set correctly?
santiw01
santiw01OP4mo ago
idk how to do that yes, i have others query and all return what it is expected
Jimmacle
Jimmacle4mo 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
santiw01OP4mo 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
Jimmacle4mo 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
santiw01OP4mo 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
Jimmacle4mo 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
SleepWellPupper
SleepWellPupper4mo ago
As an aside, I think you have to connection.OpenAsync() before querying. Also, like others have said. It seems that your nre is unrelated to the code you have shown so far.
cap5lut
cap5lut4mo ago
good call, been so long that i used databases, and if then usually efc that i dunno about how ... uuh i guess dapper(?) reacts

Did you find this page helpful?