gpt4 book ai didi

c# - 为什么我会收到 Null Reference Exception?

转载 作者:行者123 更新时间:2023-11-30 22:26:26 25 4
gpt4 key购买 nike

我正在使用 itextsharp 并尝试使用

PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

这是一个静态方法,但我总是得到这个错误

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 178:
Line 179: //create a new action to send the document to our new destination.
Line 180: PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
Line 181:
Line 182: //set the open action for our writer object


Source File: VoucherService.cs Line: 180

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
iTextSharp.text.pdf.PdfWriter.GetPageReference(Int32 page) +326
iTextSharp.text.pdf.PdfAction.GotoLocalPage(Int32 page, PdfDestination dest, PdfWriter writer) +49
OnlineStudentPlanner.Framework.Services.VoucherService.SetupPdfDoc(String invoiceNumber, String logoPath, Document doc, MemoryStream memoryStream, PdfWriter writer, PdfPTable& table) in VoucherService.cs:180
OnlineStudentPlanner.Framework.Services.VoucherService.GenerateVouchers(Int32 qty, Int32 voucherSize, String invoiceNumber, String logoPath, Int32 siteWideQty, IEnumerable`1 validDomains, Boolean siteWideVoucher) in VoucherService.cs:55
OnlineStudentPlanner.WebUI.Areas.Admin.Controllers.HomeController.GenerateVouchers(GenerateVouchersVm vm) in Admin\Controllers\HomeController.cs:52
lambda_method(Closure , ControllerBase , Object[] ) +163
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

这是我的代码

Document doc = new Document();
MemoryStream memoryStream = new MemoryStream();
PdfPTable table;

doc.Open();

PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
writer.CloseStream = false;

SetupPdfDoc(invoiceNumber, logoPath, doc, memoryStream, writer, out table);

private void SetupPdfDoc(string invoiceNumber, string logoPath, Document doc, MemoryStream memoryStream, PdfWriter writer, out PdfPTable table)
{
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 0.75f);
doc.AddTitle(String.Format("Vouchers-{0}", invoiceNumber));
}

最佳答案

这是因为您正在尝试对没有任何页面的空 PDF 文档进行操作。

更改以下三行:

        doc.Open();

PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
writer.CloseStream = false;

到:

        PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
writer.CloseStream = false;
doc.Open();
doc.NewPage();

应该可以解决您的问题。

关于c# - 为什么我会收到 Null Reference Exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11819478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com