Hope someone can spot the problem. I'm saving a DataGridView to PDF, and getting an error on the GetInstance line of code below. I've verified that sfd.FileName has a valid value and stream is not null.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
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();
}
Full method code:
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");
}
}
Stack Trace added via this code, one line before the error:
Console.WriteLine (new System.Diagnostics.StackTrace().ToString());
The thread 0x4508 has exited with code 0 (0x0). The thread 0x6078 has exited with code 0 (0x0). at Clarity.frmClarity.SavePDF() at Clarity.frmClarity.btnSavePDF_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at Clarity.Program.Main()