简体   繁体   中英

Object reference not set to an instance of an object in iTextSharp Traced in Stackify

I working on an App to create PDFs using with iTextSharp, I am getting below error:

Method: iTextSharp.text.Version.GetInstance
Error
System.NullReferenceException: Object reference not set to an instance of an object.
at iTextSharp.text.Version.GetInstance

I am quite comfortable to generate the pdf using iTextsharp, this error only traces in Stackify . Code to generate HTML to PDF as below:

XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, srHtml);

Moreover, I have latest version of iTextsharp

Install-Package iTextSharp -Version 5.5.13.1

if anyone knows or faced such type of issue then please help me:)

I have added now complete method to generate PDF with stackify snapshot :

public void GeneratePDF()
{
    try
    {
        string Pathinit = string.Empty;
        Pathinit = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["FileSystemDirectoryPath"]);
        
        var html = @"<!DOCTYPE html><html lang=""en"" xmlns=""http://www.w3.org/1999/xhtml""><head><meta charset=""utf-8"" /><title></title></head><body>";
        html += "</body></html>";
        
        Random rnd = new Random();
        int     rnd_num = rnd.Next(100000);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        pnlfrPdf.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document();

        ConsumerHelper MethodObjCH = new ConsumerHelper();

        var writer = PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath("Sample-PDF-File-" + rnd_num + ".pdf"), FileMode.Create));

        string text = string.Empty;
        int count = 0;
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            count++;
            text += line;
        }
        html = text;

        using (var srHtml = new StringReader(html))
        {
            pdfDoc.Open();
            var parserInstance = XMLWorkerHelper.GetInstance();
            if (parserInstance != null)
            {
                parserInstance.ParseXHtml(writer, pdfDoc, srHtml);
            }

            pdfDoc.Close();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

在此处输入图像描述

The method that you are calling, XMLWorkerHelper.GetInstance() is likely returning a null object. You may first want to check why that is. However, editing the code a bit will prevent this exception from cropping up.

When you try to call ParseXHtml on a null object, you get a NullReferenceException. To resolve this, you could use null checking syntax or check for null like so:

var parserInstance = XMLWorkerHelper.GetInstance();
if (parserInstance != null)
{
      parserInstance.ParseXHtml(writer, pdfDoc, srHtml);
}

The other way, which depends on the version of C# that you are using and requires the use of the question-mark to check if it is null before continuing to the following statement, like so:

XMLWorkerHelper.GetInstance()?.ParseXHtml(writer, pdfDoc, srHtml);

Feel free to try whichever method suits you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM