Tan Nguyen

Tan Nguyen

  • NA
  • 3
  • 0

System.NullReferenceExpection:Object reference not set to an instance of an object [error]

May 6 2010 11:18 PM
Im building the Form Application which seach the data from the databse and list them in the Datagrid table. But im stack with this error" System.NullReferenceExpection:Object reference not set to an instance of an object. at btnGenerateRepDate_Click"
Here is the source code of the button click:
 
private void btnGenerateRepDate_Click(object sender, EventArgs e)
        {

            if (reportPageInfo == null)
            {
                return;
            }

            int tries = 0;
            while (tries < 10)
            {

                string fileName = appDataDir + "\\ReportDateByRange" + tries.ToString() + ".PDF";
                reportPageInfo.savePath = fileName;

                try
                {
                    pdfWriter.createPDF(reportPageInfo);
                    ++tries;
                    System.Diagnostics.Process.Start(fileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            MessageBox.Show("Unable to open pdf for reading\nTry closing some open reports or try saving the pdf first", "Error", MessageBoxButtons.OK);


        }

And here is the source code of the WritePDF function

 class PDFWriter
    {
        public bool createPDF(ReportInfo pageInfo)
        {
            try
            {
                DataTable table = pageInfo.table;
                string savePath = pageInfo.savePath;
                string title = pageInfo.title;
                float[] headerWiths = pageInfo.headerWidths;

                //create document object
                Document document = new Document(PageSize.A4.Rotate(), 10, 10, 10, 40);

                try
                {
                    //create the writer from document
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(savePath, FileMode.Create));
                    MyPageEvents events = new MyPageEvents(title);
                    writer.PageEvent = events;
                    //open the document
                    document.Open();
                    // add the content to the document
                    WritePdf(writer, document, table, headerWiths, title, pageInfo.highLightRows, pageInfo.columnAlignment);
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.ToString());
                }
                document.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            return true; ;
        }


        private static void WritePdf(PdfWriter writer, Document document, DataTable table, float[] headerWidths, string title, List<int> highLightRows, List<int> columnAlignment)
        {
            int NumColumns = table.Columns.Count;
            try
            {

                // we add some meta information to the document                           
                WriteReport(document, table, NumColumns, headerWidths, highLightRows, columnAlignment);

            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.StackTrace);
            }

        }

        private static void WriteReport(Document document, DataTable table, int NumColumns, float[] headerWidths, List<int> highLightRows, List<int> columnAlignment)
        {
            PdfPTable datatable = new PdfPTable(NumColumns);

            datatable.DefaultCell.Padding = 3;
            // float[] headerwidths = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; // percentage
            if (headerWidths != null)
                datatable.SetWidths(headerWidths);
            datatable.WidthPercentage = 100; // percentage

            datatable.DefaultCell.BorderWidth = 2;
            datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;

            datatable.DefaultCell.BorderWidth = 1.0f;

            for (int i = 0; i < table.Rows.Count; i++)
            {
                // check if the row is in the list
                bool shouldHighlight = false;
                if (highLightRows != null)
                {
                    for (int t = 0; t < highLightRows.Count; ++t)
                    {
                        if (highLightRows[t] == i)
                        {
                            shouldHighlight = true;
                            highLightRows.Remove(t);
                            break;
                        }
                    }
                }

                if (shouldHighlight)
                    datatable.DefaultCell.BackgroundColor = new BaseColor(0xFFFF99);
                else
                    datatable.DefaultCell.BackgroundColor = BaseColor.WHITE;

                for (int c = 0; c < table.Columns.Count; ++c)
                {


                    datatable.DefaultCell.HorizontalAlignment = 0;

                    if (columnAlignment != null)
                    {
                        datatable.DefaultCell.HorizontalAlignment = columnAlignment[c];
                    }

                    datatable.AddCell(table.Rows[i][c].ToString());
                }
            }


            document.Add(datatable);
        }
    }

And the full project is uploaded on the attachment!

Can anyone help me to fix that error for me please!

Thanks for helping!
Tan!



Attachment: CIMS Part B.zip

Answers (3)