简体   繁体   English

itextsharp“对象引用未设置为对象的实例”错误

[英]itextsharp “Object reference not set to an instance of an object” error

I have the following code. 我有以下代码。 In my dev enviroment I am not getting any erros but in my production enviroment I do. 在我的开发环境中,我没有遇到任何错误,但是在生产环境中,我却得到了。

...

private Document _pdf;

public Report()
        {
            _pdf = new Document();
        }

public string GenerateReport(string reportType) {
        try {
            var fs = new FileStream("C:\\myfile.pdf", FileMode.Create);
           _pdfWriter = PdfWriter.GetInstance(_pdf, fs);


...

When the code run's I am getting the following error on _pdfWriter = PdfWriter.GetInstance(_pdf, fs); 当代码运行时,我在_pdfWriter = PdfWriter.GetInstance(_pdf,fs);上收到以下错误 :

Object reference not set to an instance of an object.
    at iTextSharp.text.pdf.PdfWriter.GetInstance(Document document, Stream os)
    at Report.GenerateReport(String reportType)

Why do you think I am getting ths error? 为什么您认为我遇到了错误? The filestream has been created and the _pdf is set in the constructor. 已创建文件流,并在构造函数中设置了_pdf。

Update 更新资料

The problem is the _pdf is null. 问题是_pdf为空。 I am not sure why it is null as set in the constructor. 我不确定为什么它在构造函数中设置为null。 I can get round this problem by doing: 我可以通过以下方法解决此问题:

if (_pdf == null) {
    _pdf = new Document();
}
_pdfWriter = PdfWriter.GetInstance(_pdf, fs);

I would still like to know what I am doing wrong... 我仍然想知道我在做什么错...

该问题与生产中的超时问题有关,因为有更多数据。

当我打开所有例外时,我遇到了同样的问题,但是您可以继续解决该例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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