Roustam Akhmetov

Roustam Akhmetov

  • 2.1k
  • 12
  • 606

Object reference not set to an instance of an object.

Jun 4 2022 2:53 AM

Hi,

I have this code which is running fine in my console project:

C#:

public async Task Insert_BalanceSheet(DateTime reportDate, int entityID, DateTime periodStart, DateTime periodEnd, string timeFrame, AccountingApi apiInstance, string xeroTenantId, string accessToken, string dataDB)
{
    ...
    try
    {
        Xero.NetStandard.OAuth2.Model.Accounting.ReportWithRows pal = await apiInstance.GetReportBalanceSheetAsync(accessToken, xeroTenantId, startDate, 1, timeFrame, null, null, true, false);           
        fullReport = JsonSerializer.Serialize(pal);
    }
    catch (Exception e)
    {
        Console.WriteLine("Failed to obtain Xero Balance Sheet report for entity: " + entityID + " start date: " + startDate + " end date: " + endDate);
        Console.WriteLine("IOException source: {0}", e.Source);
        Console.WriteLine(e.ToString());
    }
            ...
}

When I call it from a console project it works fine. When I call this from a web project, it crashes on this line:
Xero.NetStandard.OAuth2.Model.Accounting.ReportWithRows pal = await apiInstance.GetReportBalanceSheetAsync(accessToken, xeroTenantId, startDate, 1, timeFrame, null, null, true, false);
with this error:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>

It does not even go the catch part of the exception, so I don't get an insight of what is going on there.

Any ideas?

Thanks.


Answers (2)