title: Why am I getting a Null Reference Exception?
body: <p>I am using itextsharp and trying to use </p>
  
  <pre><code>PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
  </code></pre>
  
  <p>which is a static method but I always get this error</p>
  
  <pre><code>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&amp; 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.&lt;&gt;c__DisplayClass15.&lt;InvokeActionMethodWithFilters&gt;b__12() +55
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
     System.Web.Mvc.&lt;&gt;c__DisplayClass17.&lt;InvokeActionMethodWithFilters&gt;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.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() +37
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +21
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +12
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
     System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +50
     System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;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&amp; completedSynchronously) +184
  </code></pre>
  
  <p>here is my code that I have</p>
  
  <pre><code>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));
  }
  </code></pre>