找不到空引用异常 [重复]

Can't find null reference exception [duplicate]

提问人:Jtara 提问时间:4/20/2023 更新时间:4/20/2023 访问量:109

问:

我有另一个项目,其设置几乎完全相同,工作正常。我不知道这个空引用是从哪里来的。当我设置断点并在 VS 中查看它们时,签名中没有一个变量是空的。

调用堆栈

ReportBuilder.Application.dll!ReportBuilder.Application.Templates.TemplateHelper.GetTemplateReport(System.Collections.Generic.IEnumerable template, int fiscalYear, int period, System.Threading.CancellationToken cancellationToken) 第 40 行 C# ReportBuilder.API.dll!ReportBuilder.API.Controllers.TemplateController.GetTemplateReport(int FiscalYear, int Period, System.Collections.Generic.IEnumerable Sites, System.Threading.CancellationToken cancellationToken) 第 39 行 C# [轻量级功能]
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultTypeMapper 映射器, Microsoft.Extensions.Internal.ObjectMethodExecutor executor, 对象控制器, object[] 参数) 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync.__Logged|12_1(Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker invoker) 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.State next, ref Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Scope scope, ref object state, ref bool isCompleted) 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.State next, ref Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Scope scope, ref object state, ref bool isCompleted) 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.State next, ref Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Scope scope, ref object state, ref bool isCompleted) 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.State next, ref Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Scope scope, ref object state, ref bool isCompleted) 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResourceFilter() 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.State next, ref Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Scope scope, ref object state, ref bool isCompleted) 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeAsync.__Logged|17_1(Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker invoker) 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeAsync() 未知 Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Routing.ControllerRequestDelegateFactory.CreateRequestDelegate.AnonymousMethod__0(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Routing.dll!Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) 未知 服务器共享.dll!Server.Shared.Services.RequestLogContextMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Authorization.Policy.dll!Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Authentication.dll!Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Routing.dll!Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) 未知 Microsoft.AspNetCore.StaticFiles.dll!Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Http.Abstractions.dll!Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Http.Abstractions.dll!Microsoft.AspNetCore.Builder.UseExtensions.Use.AnonymousMethod__2() 未知 服务器共享.dll!Server.Shared.Services.ServiceExtensions.AddSharedConfiguration.AnonymousMethod__4_2(Microsoft.AspNetCore.Http.HttpContext 上下文、System.Func<System.Threading.Tasks.Task> next) 未知 Microsoft.AspNetCore.Http.Abstractions.dll!Microsoft.AspNetCore.Builder.UseExtensions.Use.AnonymousMethod__1(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.HttpsPolicy.dll!Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.StaticFiles.dll!Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.StaticFiles.dll!Microsoft.AspNetCore.StaticFiles.DefaultFilesMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Diagnostics.dll!Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Http.Abstractions.dll!Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Diagnostics.dll!Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.ResponseCompression.dll!Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.InvokeCore(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.ResponseCompression.dll!Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.StaticFiles.dll!Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Swashbuckle.AspNetCore.SwaggerUI.dll!Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) 未知 Swashbuckle.AspNetCore.Swagger.dll!Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext, Swashbuckle.AspNetCore.Swagger.ISwaggerProvider swaggerProvider) 未知 Microsoft.AspNetCore.Http.Abstractions.dll!Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware.AnonymousMethod__2(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Diagnostics.dll!Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.HostFiltering.dll!Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.WebTools.BrowserLink.Net.dll!Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.WebTools.BrowserLink.Net.dll!Microsoft.WebTools.BrowserLink.Net.VsContentMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Watch.BrowserRefresh.dll!Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Http.Abstractions.dll!Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) 未知 Microsoft.AspNetCore.Hosting.dll!Microsoft.AspNetCore.Hosting.HostingApplication.ProcessRequestAsync(Microsoft.AspNetCore.Hosting.HostingApplication.Context context) 未知 Microsoft.AspNetCore.Server.IIS.dll!Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT<Microsoft.AspNetCore.Hosting.HostingApplication.Context>。ProcessRequestAsync() 未知 Microsoft.AspNetCore.Server.IIS.dll!Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.HandleRequest() 未知 Microsoft.AspNetCore.Server.IIS.dll!Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.Execute() 未知 System.Private.CoreLib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() 未知 System.Private.CoreLib.dll!System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() 未知 System.Private.CoreLib.dll!System.Threading.Thread.StartCallback() 未知 调用堆栈

public async Task<byte[]> GetTemplateReport(IEnumerable<long> template, int fiscalYear, int period, CancellationToken cancellationToken = default)
    {
        Console.WriteLine("");
        return await _ReportHelper.GetReportBytes(new TemplateReport(fiscalYear, period, template, _Mediator), cancellationToken);
    }
public async Task<byte[]> GetReportBytes(IReport report, CancellationToken cancellationToken = default(CancellationToken))
        {
            IReport report2 = report;
            IReportBuilder reportBuilder = _ReportBuilder;
            reportBuilder.SetData(await report2.GetReportData(cancellationToken));
            TemplateBuilder templateBuilder = new TemplateBuilder();
            templateBuilder.GetReportOptions(report2.GetReportOptions(new ReportOptions()));
            templateBuilder.SetReportHeaders(report2.GetReportHeaders(new List<ReportHeader>()));
            foreach (ReportColumnSection reportColumnSection in report2.GetReportColumnSections(new ReportColumnSectionsBuilder()))
            {
                templateBuilder.AddColumnSection(reportColumnSection);
            }

            foreach (ReportRowSection reportRowSection in report2.GetReportRowSections(new ReportRowSectionsBuilder()))
            {
                templateBuilder.AddReportSection(reportRowSection);
            }

            templateBuilder.SetRowFormulas(report2.GetRowFormulas(new List<RowFormulaConfig>()));
            _ReportBuilder.SetConfiguration(templateBuilder.Build());
            _ReportBuilder.SetFinalizeCallBack(delegate (IWorksheet x)
            {
                report2.ReportFinalize(x);
            });
            return _ReportBuilder.BuildByteArray();
        }
using ReportBuilder.Application.Templates.Queries;
using GrapeCity.Documents.Excel;
using MediatR;
using Reporting.Shared;

namespace ReportBuilder.Application.Templates.Reports;
public class TemplateReport : IReport
{
    private readonly IMediator _Mediator;
    private int FiscalYear;
    private int Period;
    private IEnumerable<long> Templates = Enumerable.Empty<long>();

    public TemplateReport(int fiscalYear, int period, IEnumerable<long> templates, IMediator mediator)
    {
        _Mediator = mediator;
        FiscalYear = fiscalYear;
        Period = period;
        Templates = templates;
    }
    

  


C# ASP.NET-WEB-API NET-6.0 NullReferenceException 调解器

评论

1赞 shingo 4/20/2023
调试器应告诉您哪一个为 null。

答:

0赞 Jtara 4/20/2023 #1
public TemplateHelper(IMediator mediator, IReportHelper reportHelper)
    {
        _Mediator = mediator;
        _ReportHelper = reportHelper;
    }

我在其他地方的签名中缺少那个 IReportHelper。不知道为什么它不能告诉我。