0

I don't think I have seen this type of error in C# before and I have been using C# for years, but then again, I am new to Aysnc and Task in C# so maybe it's related. I have a method declared as follows:

private async Task MyMethod(...)

I call this method as follows:

await MyMethod(...)

Inside MyMethod(...), I am making some calls to other synchronous methods, but I believe that is allowed in C#, right? In any case, when debugging inside MyMethod(...) with Visual Studio, I can also see my synchronous methods completing normally.

So everything seems to be going well in MyMethod(...), until the final closing curly bracket is hit, where MyMethod(...) should be exiting:

enter image description here

When the final curly bracket is hit to exit MyMethod(...), I get a System.NullReferenceException. Does anybody know what is going on and why I am getting a System.NullReferenceException?

Edit: If I make MyMethod(...) a normal synchronous method (ie. I remove async Task, and I don't call await on it), then it can complete normally with no exceptions and no errors.

Programmer Joe
  • 104
  • 1
  • 8
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Fildor Jun 21 '23 at 13:48
  • Not really. That's just a very general answer on null exceptions. Some object is null, I know that. But with respect to my question, the final ending curly bracket has been reached in the Visual Studio debugger and up to that point, there has not been a null exception detected. Is extra code executed by the closing curly bracket? I even tried to "step in" using the debugger but the closing curly bracket still triggers the null exception. My issue may be related to Async and Task. That article you suggested does not even mention Async and Task at all. – Programmer Joe Jun 21 '23 at 14:15
  • 1
    No one could tell you without seeing the code in the method. The debugger could be pointing at the wrong line (try Build menu -> Clean solution and running it again). Or maybe you have a `using` in your method on an object that ends up being null? – Gabriel Luci Jun 21 '23 at 14:16
  • @GabrielLuci Yeah, I'm thinking Visual Studio could be showing me the wrong line, that would be somewhat logical. I am not using "using" at all in my MyMethod(...), but even if I were, I would think that the null exception would be caught within the "using" block, not at the final curly bracket when the method is exiting. – Programmer Joe Jun 21 '23 at 14:20
  • 3
    It's possible that a `Task`-returning method is returning a `null` reference instead of a `Task` instance, which is a common mistake when the method is not marked `async` and returns `null` instead of `Task.FromResult(null)`. Need to see the code. – madreflection Jun 21 '23 at 14:25
  • 2
    OP, if you're using ASP.NET WebForms (and no-one should be...) and you want to use `async` methods then you need to ensure you have `Async="true"` in your `@Page` directive _and_ your `web.config` has `aspnet:UseTaskFriendlySynchronizationContext` set to `true` _and_ your async code is only invoked via `RegisterAsyncTask` (you can't use `async` methods from WebForms "server-side events"). See here: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45 – Dai Jun 21 '23 at 14:48
  • @ProgrammerJoe how is your method called? And what is the actual *full* exception text, including the call stack? You can get that easily by clicking on `Copy Details`. I suspect whatever calls `await MyMethod()` isn't awaited itself, so when the runtime tries to get back to the original sync context, there's nothing to return to – Panagiotis Kanavos Jun 21 '23 at 14:49
  • 2
    Please post enough code to reproduce the problem. `await` works fine - all ASP.NET Core web apps depend on it. ASP.NET MVC also worked fine once MVC 4 came out – Panagiotis Kanavos Jun 21 '23 at 14:49

0 Answers0