I got the object null error with following script in C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Spreadsheet;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using ProSmart.Core;
using ProSmart.Dtos.EntityDto.AuthService;
using ProSmart.Dtos.EntityDto.ConfigurationService;
using ProSmart.Dtos.EntityDto.SupplierService;
using ProSmart.Dtos.EntityDto.TenderingService;
using ProSmart.Dtos.Enumurable.SupplierServices;
using ProSmart.Dtos.EventDtos.ActionEvent.FileServices;
using ProSmart.Dtos.EventDtos.QueryEvent.ReportingService;
using ProSmart.ReportingServices.Handlers;
using ProSmart.ReportingServices.Helper;
using ProSmart.ReportingServices.Modules;

namespace ProSmart.ReportingServices.Reports
{
    internal class SupplierMasterReport : ReportBase
    {
        private ReportingServicesDbTransactionHelper dbHelper;
        public SupplierMasterReport(FileHandler fileHandler, BucketPathHandler bucketPathHandler, ReportingServicesDbContext dbContext, GenerateReportQueryDto dto, string userId)
            : base(fileHandler, bucketPathHandler, dbContext, dto, userId)
        {
            contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        }

        /*
          {"timestamp":"2025-04-11T02:00:19.951Z",
          "data":{"generateReportQuery":{"reportType":"SupplierMaster","objectId":"YWQ2ODRiOTEtMTE164UEGBGR+yHDb3eS16/L9Cb47tarDLrXd5V+dTSqVa2XdUEeE5GItw=="}}}
         */
        public async Task<ProFileItemUpdateDto> Create()
        {
            var filter = BsonDocument.Parse(_dto.JsonQuery);

            var mongoClient = (_dbContext as AbstractDbContext).MongoClient;
            //Convert all category id with ObjectID support
            // Navigate to the $in array in the document structure 
            if (filter.Contains("$and"))
            {
                var andArray = filter["$and"]?.AsBsonArray ?? null;
                // Iterate through each condition to find "Categories"
                if (andArray != null)
                {
                    foreach (var condition in andArray)
                    {
                        var condDoc = condition?.AsBsonDocument ?? null;

                        if (condDoc.Contains("Categories"))
                        {
                            var categories = condDoc["Categories"]?.AsBsonDocument ?? null;

                            if (categories.Contains("$elemMatch"))
                            {
                                var elemMatch = categories["$elemMatch"]?.AsBsonDocument ?? null;

                                if (elemMatch.Contains("CategoryId"))
                                {
                                    var categoryId = elemMatch["CategoryId"]?.AsBsonDocument ?? null;

                                    if (categoryId.Contains("$in"))
                                    {
                                        var inArray = categoryId["$in"]?.AsBsonArray ?? null;

                                        // Convert string IDs to ObjectId and replace $in array
                                        var objectIdArray = new BsonArray(inArray.Select(id => new ObjectId(id.AsString)));
                                        categoryId["$in"] = objectIdArray;
                                    }
                                }
                            }
                        }
                    }
                }
            }


            DateTimeConvertor.ReplaceIsoDateStringsInDocument(filter);

            //throw new Exception(filter.ToJson(new JsonWriterSettings { Indent = true }));

            var result = mongoClient.GetDatabase("SupplierServices").GetCollection<BsonDocument>("ProSupplierMasterInfo").Find(filter).ToList();
            var entities = new List<ProSupplierMasterInfoEntityDto>();

            foreach (var r in result)
            {
                entities.Add(BsonSerializer.Deserialize<ProSupplierMasterInfoEntityDto>(r));
            }

            entities = entities.OrderByDescending(item => item.DateModified).ToList();

            dbHelper = new ReportingServicesDbTransactionHelper(_dbContext);

            var xlsgen = new ExcelReportGenerator();
            if (entities != null)
            {
                FillExcel(xlsgen, entities);
            }
            using (MemoryStream outputStream = new MemoryStream())
            {
                xlsgen.Save(outputStream);
                var proFileDto = await SendToMinio(outputStream, "SupplierMaster.xlsx");
                return proFileDto;
            }

        }

        private void FillExcel(ExcelReportGenerator xlsgen, List<ProSupplierMasterInfoEntityDto> entities)
        {
            var columnTypes = new List<Type> { };
            var supplierFields = new List<string>
            {
                "Supplier Classification",
                "Supplier Type",
                "Supplier Name",
                "FMS Supplier Number",
                "EPRO Supplier Number",
                "Business Registration Number",
                "Business Registration Number Expiry Date",
                "Business Registration Region",
                "Company Address",
                "Contact Person Name",
                "Contact Person Position",
                "Contact Email",
                "Contact Phone Number (Office)",
                "Fax Number",
                "Procurement Category 1",
                "Procurement Category 2",
                "Procurement Category 3",
                "Pre-Registration Invited by Buyer Date",
                "Buyer and Position for Pre-Registration Invitation",
                "Pre-Registration Submitted by Supplier Date",
                "Pre-Registration Checked by Buyer Date",
                "Full Registration Invited by Buyer Date",
                "Buyer and Position for Full Registration Invitation",
                "Full Registration Submitted by Supplier Date",
                "Full Registration Checked by Buyer Date",
                //"Full Registration Approved in FMS Date",
                "Suspension Start Date",
                "Suspension End Date",
                "Suspension Remarks",
                "Overall Performance Score",
                "Termination Start Date",
                "Termination Remarks"
            };
            xlsgen.CreateHeader(supplierFields);

            foreach (var tableCol in supplierFields)
            {
                columnTypes.Add(typeof(string));
            }

            var table = new List<List<string>>();

            foreach (var entity in entities)
            {
                if (entity != null)
                {
                    var curEntityData = ConstructTableFromData(entity);
                    table.AddRange(curEntityData);
                }
            }

            xlsgen.FillTable(table, columnTypes);
        }

        private List<List<string>> ConstructTableFromData(ProSupplierMasterInfoEntityDto entity)
        {
            var rows = new List<List<string>>();
            
            // build up the table
            var supplierSuspendData = dbHelper.GetReadOnlyCollectionQuery<ProSupplierSuspendEntityDto>().LastOrDefault(item => item.SupplierId == entity.Id);
            var supplierTerminationData = dbHelper.GetReadOnlyCollectionQuery<ProSupplierTerminationEntityDto>().LastOrDefault(item => item.SupplierId == entity.Id);
            var supplierInvitedData = dbHelper.GetReadOnlyCollectionQuery<ProInviteUserEntityDto>().FirstOrDefault(item => item.UserId == entity.Id);
            var addr = entity?.ContactPerson != null ? 
                (entity?.ContactPerson?.Addr1 + 
                ((entity?.ContactPerson?.Addr2 != null || entity?.ContactPerson?.Addr2 != "") ? (", " + entity?.ContactPerson?.Addr2) : "" ?? "") +
                ((entity?.ContactPerson?.Addr3 != null || entity?.ContactPerson?.Addr3 != "") ? (", " + entity?.ContactPerson?.Addr3) : "" ?? "") +
                ((entity?.ContactPerson?.Addr4 != null || entity?.ContactPerson?.Addr4 != "") ? (", " + entity?.ContactPerson?.Addr4) : "" ?? "")) :
                "";
            var category1 = ""; 
            var category2 = "";
            var category3 = "";
            
            if (entity.Categories != null)
            {
                if (entity.Categories?.Count > 0 && entity.Categories[0] != null)
                {
                    ProCategoryEntityDto catData = dbHelper.GetReadOnlyCollectionQuery<ProCategoryEntityDto>().FirstOrDefault(item => item.Id == entity.Categories[0].CategoryId);
                    if (catData != null) category1 = catData?.Description.Eng != null ? catData?.Description.Eng : catData?.FMSSupplierCategory;
                }
                if (entity.Categories?.Count > 1 && entity.Categories[1] != null)
                {
                    ProCategoryEntityDto catData = dbHelper.GetReadOnlyCollectionQuery<ProCategoryEntityDto>().FirstOrDefault(item => item.Id == entity.Categories[1].CategoryId);
                    if (catData != null) category2 = catData?.Description.Eng != null ? catData?.Description.Eng : catData?.FMSSupplierCategory;
                }
                if (entity.Categories?.Count > 2 && entity.Categories[2] != null)
                {
                    ProCategoryEntityDto catData = dbHelper.GetReadOnlyCollectionQuery<ProCategoryEntityDto>().FirstOrDefault(item => item.Id == entity.Categories[2].CategoryId);
                    if (catData != null) category3 = catData?.Description.Eng != null ? catData?.Description.Eng : catData?.FMSSupplierCategory;
                }
            }
            

            var inviter = new List<Dtos.Base.UserInfoBase>();
            if(entity.Inviter != null)
            {
                inviter = entity.Inviter;
            }

            var brExpireDate = entity.BrExpiryDate != null ?
                    (entity.BrExpiryDate.Year.ToString() +
                    (entity.BrExpiryDate.Month != null ? ("/" + (entity.BrExpiryDate.Month + 1)) : "") +
                    (entity.BrExpiryDate.Day != null ? ("/" + entity.BrExpiryDate.Day) : ""))
                    : "";

            var row = new List<string>()
            {
                GetEnumDescription(entity.RegistrationType == RegistrationTypeEnum.PreRegistration ? entity.RegistrationType : entity.ClassificationIndicator) ?? "", // Supplier Type
                GetEnumDescription(entity.Status) ?? "",// Supplier Status
                entity.CompanyName ?? "", // Supplier Name
                entity.FmsSupplierId ?? "",// FMS Supplier Number
                entity.SupplierCode ?? "",// EPRO Supplier Number
                entity.BrCertificateNo ?? "",// Business Registration Number
                brExpireDate ?? "",// Business Registration Number Expiry Date
                entity.RegisteredCountry ?? "",// Business Registration Region
                addr ?? "",// Company Address
                entity.ContactPerson ?.FirstName + (entity.ContactPerson.LastName != null ? ", " + entity.ContactPerson.LastName : "") ?? "",// Contact Person Name
                entity.ContactPerson ?.Position ?? "", // Contact Person Position
                entity.ContactPerson ?.EmailAddress ?? "", // Contact Email
                entity.ContactPerson ?.Tel ?.TelNo ?? "", // Contact Phone Number (Office)
                entity.ContactPerson ?.Fax ?.TelNo ?? "", // Fax Number
                category1 ?? "", // Procurement Category 1
                category2 ?? "", // Procurement Category 2
                category3 ?? "", // Procurement Category 3
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(entity.DateCreated) ?? "", //Pre-Registration Invited by Buyer Date
                inviter.Count > 0 ? (inviter[0] ?.UserName) : "" ?? "", // Buyer and Position for Pre-Registration Invitation
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(entity.PreRegistrationFirstSubmissionDate) ?? "", // Pre-Registration Submitted by Supplier Date
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(entity.PreRegistrationApprovalDate) ?? "", // Pre-Registration Checked by Buyer Date
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(supplierInvitedData?.DateCreated) ?? "", // Full Registration Invited by Buyer Date
                inviter.Count > 0 ? (inviter[0] ?.UserName) : "" ?? "", // Buyer and Position for Full Registration Invitation
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(entity.FullRegistrationFirstSubmissionDate) ?? "",// Full Registration Submitted by Supplier Date
                //"",// TODO: Full Registration Checked by Buyer Date
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(entity.FullRegistrationApprovalDate) ?? "",// Full Registration Approved in FMS Date
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(supplierSuspendData?.StartDateTime) ?? "",// Suspension Start Date
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(supplierSuspendData?.EndDateTime) ?? "",// Suspension End Date
                supplierSuspendData?.Reason ?? "",// Suspension Remarks
                entity.PerformanceScore ?.ToString() ?? "", // Overall Performance Score
                DateTimeConvertor.convertTimeToTimeZoneWithFormat(supplierTerminationData?.StartDateTime) ?? "",// Termination Start Date
                supplierTerminationData?.Reason ?? ""// Termination Remarks
            };
            rows.Add(row);
            return rows;
        }
    }
}


Can you fix the null check?