Another "Object not set to the instance of an object" Error

218 views Asked by At

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!

1

There are 1 answers

6
Daniel Lorenz On
 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.