C# iTextSharp GetInstance-NullReferenceException错误
希望有人能发现问题。我正在将DataGridView保存为PDF,下面的GetInstance代码行出现错误。我已经验证了sfd.FileName具有有效值,并且流不为null System.NullReferenceException:“对象引用未设置为对象的实例。”C# iTextSharp GetInstance-NullReferenceException错误,c#,itext,nullreferenceexception,C#,Itext,Nullreferenceexception,希望有人能发现问题。我正在将DataGridView保存为PDF,下面的GetInstance代码行出现错误。我已经验证了sfd.FileName具有有效值,并且流不为null System.NullReferenceException:“对象引用未设置为对象的实例。” using (FileStream stream = new FileStream (sfd.FileName, FileMode.Create)) { iTextSharp.text.Document pdfDoc =
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_点击(对象发送者,事件参数e)在
System.Windows.Forms.Control.OnClick(EventArgs e)位于
System.Windows.Forms.Button.OnClick(EventArgs e)位于
System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)位于
System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons
按钮,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.UnsafentiveMethods.DispatchMessageW(MSG&MSG)在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32原因、Int32 pvLoopData)位于 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,应用程序上下文上下文)位于 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext上下文)位于Clarity.Program.Main()处
我找到了解决办法,尽管我不理解。您必须在调试选项下打开“仅我的代码”。我这样做之后,错误消失了。您能分享完整的堆栈跟踪吗?嗨,mkl。刚刚添加到我的原始帖子中。您可能需要提供完全限定的路径:iTextSharp.text.pdf.PdfWriter.GetInstance()。我猜编译器认为PdfWriter应该是一个未初始化的对象。看这里:我这样做了,但仍然得到相同的错误(我对.NET不太了解,但可能您是在调试模式下运行代码?Visual Studio对此有点奇怪?您能在JetBrains Rider中重现这个问题吗?这是我比较熟悉的IDE。