-1

I have been struggling with this error for a few days now. Hopefully you can see what I cannot and help point me in the right direction.

private void FillFormChg()
{
    pdfTemplate = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Slip.pdf");
    newFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "SlipTemp.pdf");

    try
    {
        PdfReader pdfReader = new PdfReader(pdfTemplate);
        
        if (pdfTemplate == null && newFile == null)
        {
            MessageBox.Show("Can't find Templates!");
            return;
        }
        
        PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.OpenOrCreate));
        pdfStamper.Writer.CloseStream = false;
        AcroFields pdfFormFields = pdfStamper.AcroFields;
    }
}

I have checked for null, and made sure that the templates are there and readable, but I still get the error on the PdfStamper line. Thank you in advance for your insight!

Jannick Breunis
  • 195
  • 2
  • 14
s.d.c321
  • 3
  • 4
  • What line does the exception occur on? – canton7 May 17 '21 at 14:53
  • PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.OpenOrCreate)); – s.d.c321 May 17 '21 at 14:54
  • Consider looking at the stack trace of the error to determine details. If it is not clear to you, consider posting it here. – mkl May 17 '21 at 15:00
  • I appreciate your help, I understand the reason behind the error but not what is causing it. – s.d.c321 May 17 '21 at 15:00
  • 1
    What is the full `ToString()` output of the exception including the exception type, message, traceback and inner exception(s) if any? – dbc May 17 '21 at 15:15
  • Stack Trace- System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=itextsharp StackTrace: at iTextSharp.text.Version.GetInstance() – s.d.c321 May 17 '21 at 15:40
  • ` System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=itextsharp StackTrace: at iTextSharp.text.Version.GetInstance() ` – s.d.c321 May 17 '21 at 15:42
  • Please [edit] your question to include the full message, including **all** entries in the stack trace – canton7 May 17 '21 at 15:45
  • So how do capture all entries of the stack trace? – s.d.c321 May 17 '21 at 16:13
  • For future reference, I discovered that although I had added iTextSharp through NuGet, I failed to create a reference to it.... Once I had the reference it worked beautifully! – s.d.c321 May 17 '21 at 21:59

1 Answers1

1
 if (pdfTemplate == null && newFile == null)

This should probably be an || and not an &&. If one of those files is null, but not both, then it would crash later if pdfTemplate was null and newFile was not.

Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39
  • 1
    Also: `PdfReader pdfReader = new PdfReader(pdfTemplate);` uses `pdfTemplate` before the check. – Fildor May 17 '21 at 15:00
  • Now that makes sense to me. I will make changes and give it a go. Thank you Fildor! – s.d.c321 May 17 '21 at 15:04
  • Same error, after moving the PdfReader pdfReader = new PdfReader(pdfTemplate); past the template null check. – s.d.c321 May 17 '21 at 15:06
  • @s.d.c321 I actually doubt moving down that line will remedy the error, despite being "more correct". I am not sure what happens if the argument to `new PdfReader` is not a null string but the file _does not exist_ ... – Fildor May 17 '21 at 15:06
  • @Fildor I have another method the creates the templates before this method is called. I know they exist in the right location and are readable. – s.d.c321 May 17 '21 at 15:09
  • Hm, then I honestly do not know what else _could_ be null in that line. I'd step it through in the debugger and check any variables on the way. – Fildor May 17 '21 at 15:11