为什么我得到一个空引用异常?
问题描述:
我使用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();
应该解决您的问题。
+0
我会尝试,但如果我移动我的上层代码到相同的方法。这一切正常,我不需要做NewPage(); – chobo2 2012-08-05 20:24:12
+0
在这种情况下,请尝试删除PDFTable输出参数。这是我能看到的唯一可能导致这种行为的事情。 – 2012-08-05 22:36:48
答
你检查过pdfDest和writer是否为空?它以线抛出异常:
iTextSharp.text.pdf.PdfWriter.GetPageReference(Int32 page) +326
我认为作家不为空,所以无论是pdfDest为空或有缺陷的iTextSharp的,并传递错误的页面是引起异常(错误的例外,它应该是OutOfBounds或者其他的东西)。
'doc'和'memoryStream'是空的。这是预期的吗? – 2012-08-05 19:40:50