【问题标题】:iTextSharp GetInstance - NullReferenceException erroriTextSharp GetInstance - NullReferenceException 错误
【发布时间】:2021-01-17 05:48:12
【问题描述】:

希望有人能发现问题。我正在将 DataGridView 保存为 PDF,并在下面的 GetInstance 代码行中出现错误。我已验证 sfd.FileName 具有有效值并且流不为空。

System.NullReferenceException: '对象引用未设置为对象的实例。'

using (FileStream stream = new FileStream (sfd.FileName, FileMode.Create)) {

   iTextSharp.text.Document pdfDoc =  new iTextSharp.text.Document (PageSize.A4, 10f, 20f, 20f, 10f);

   PdfWriter.GetInstance (pdfDoc, stream);   // NullReferenceException on this line.
   pdfDoc.Open();
   pdfDoc.Add (pdfTable);
   pdfDoc.Close();
   stream.Close();
}

完整方法代码:

private void SavePDF () {

    if (grdKeywordSearch.Rows.Count > 0) {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "PDF (*.pdf)|*.pdf";
        sfd.FileName = "Output.pdf";
        bool fileError = false;
        if (sfd.ShowDialog () == DialogResult.OK) {
            if (File.Exists (sfd.FileName)) {
                try {
                    File.Delete (sfd.FileName);
                } catch (IOException ex) {
                    fileError = true;
                    MessageBox.Show ("It wasn't possible to write the data to the disk." + ex.Message);
                }
            }
            if (!fileError) {
                try {
                    PdfPTable pdfTable = new PdfPTable (grdKeywordSearch.Columns.Count);
                    pdfTable.DefaultCell.Padding = 3;
                    pdfTable.WidthPercentage = 100;
                    pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;

                    foreach (DataGridViewColumn column in grdKeywordSearch.Columns) {
                        PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
                        pdfTable.AddCell (cell);
                    }

                    foreach (DataGridViewRow row in grdKeywordSearch.Rows) {
                        foreach (DataGridViewCell cell in row.Cells) {
                            pdfTable.AddCell (cell.Value.ToString());
                        }
                    }

                    using (FileStream stream = new FileStream (sfd.FileName, FileMode.Create)) {

                        iTextSharp.text.Document pdfDoc =  new iTextSharp.text.Document (PageSize.A4, 10f, 20f, 20f, 10f);

                        PdfWriter.GetInstance (pdfDoc, stream);
                        pdfDoc.Open();
                        pdfDoc.Add (pdfTable);
                        pdfDoc.Close();
                        stream.Close();
                    }

                    MessageBox.Show ("Data Exported Successfully.", "Info");
                } catch (Exception ex) {
                    MessageBox.Show ("Error :" + ex.Message);
                }
            }
        }
    } else {
        MessageBox.Show ("Nothing to export.", "Info");
    }

}

通过此代码添加的堆栈跟踪,错误前一行:

Console.WriteLine (new System.Diagnostics.StackTrace().ToString());

线程 0x4508 已退出,代码为 0 (0x0)。线程 0x6078 有 以代码 0 (0x0) 退出。在 Clarity.frmClarity.SavePDF() 在 Clarity.frmClarity.btnSavePDF_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,鼠标按钮 按钮,Int32 点击)在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg,IntPtr wparam,IntPtr lparam)在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 原因,Int32 pvLoopData)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文)在 Clarity.Program.Main()

【问题讨论】:

  • 你能分享完整的堆栈跟踪吗?
  • 嗨,mkl。刚刚添加到我的原始帖子中。
  • 您可能需要提供完全限定的路径:iTextSharp.text.pdf.PdfWriter.GetInstance()。我的猜测是编译器认为 PdfWriter 应该是一个未初始化的对象。见这里:iTextSharp pdfWriter.GetInstance error
  • 我这样做了,但仍然得到同样的错误。 :(

标签: c# itext nullreferenceexception


【解决方案1】:

我找到了解决方案,虽然我不明白。您必须在调试选项下打开“仅我的代码”。我这样做后错误就消失了。

【讨论】:

猜你喜欢
  • 2013-03-06
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
相关资源
最近更新 更多