Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Void MoveNext() in Null reference exception

Tags:

c#

I have a WebAPI which is having the following piece of code in which there is Null reference exception in the code written in try block which is getting logged using my logger.

But in the TargetSite of the Exception getting logged, I am receiving Void MoveNext() instead of the method name in which this code is written.

What could be the reason for the same??

public async Task<ResponseStatus> ProcessRCSCNotification(IList<RecurringSubscriptionModelV2> recurringSubscriptionList, string appCD)
    {
            foreach (var model in modelList)
{
  // Some code
  try
  {
     // Exception occurs here
  }
  catch (Exception ex)
  {
    // Logger is logging exception here
  }
}
        return null;
    }
like image 965
Ipsit Gaur Avatar asked Jun 05 '26 12:06

Ipsit Gaur


2 Answers

You have an async method with several awaits. The compiler transform this whole method into a state machine and your method ends up actually only calling this state machine.

This state machine class has the mentioned MoveNext() method that now contains all the work you actually wanted to do.

To analyze your NullReferenceException you should rather check the StackTrace property of the exception than the TargetSite.

like image 142
René Vogt Avatar answered Jun 07 '26 13:06

René Vogt


The method is an async/await method.

This kind of method is rewritten to a state machine with a MoveNext method. That's why your stack trace will identify this method as the one throwing that exception.

.NET Core 2.0 or 2.1 have either built-in or an extra nuget package that will fix stack traces like this to mention the actual method instead of the generated one.

You can read more about this here: Age of Ascent: Stacktrace improvements in .NET Core 2.1.

It might not fixup the TargetSite however, and I don't think it will handle .NET Framework.

like image 32
Lasse V. Karlsen Avatar answered Jun 07 '26 14:06

Lasse V. Karlsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!