Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is anybody can help me understand the issue of my code? It is always going to the exception, does not matter if I try to "treat" the cell with NULL.

My catch System.Exception error is:
"Object reference not set to an instance of an object"



C#
try
 {

     worksheet = workbook.ActiveSheet;

     worksheet.Name = "ExportedFromDatGrid";

     int cellRowIndex = 1;
     int cellColumnIndex = 1;


     //Loop through each row and read value from each column.
     for (int i = -1; i < dataGridView1.Rows.Count - 1; i++)
     {
         for (int j = 0; j < dataGridView1.Columns.Count; j++)
         {
             // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
             if (cellRowIndex == 1)
             {
                 worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Columns[j].HeaderText;
             }
             else
             {

                 if (dataGridView1.Rows[i].Cells[j].Value.ToString() != null)
                 {
                     worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                 }
                 else
                 {
                     worksheet.Cells[cellRowIndex, cellColumnIndex] = String.Empty;
                 }


                 // working:
                 //worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();
             }
             cellColumnIndex++;
         }
         cellColumnIndex = 1;
         cellRowIndex++;
     }

     //Getting the location and file name of the excel to save from user.
     SaveFileDialog saveDialog = new SaveFileDialog();
     saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
     saveDialog.FilterIndex = 2;

     if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         workbook.SaveAs(saveDialog.FileName);
         MessageBox.Show("Export Successful");
     }
 }
 catch (System.Exception ex)
 {
     MessageBox.Show(ex.Message);
 }
 finally
 {
     excel.Quit();
     workbook = null;
     excel = null;
 }


What I have tried:

I tried, with no success, to use an IF/ELSE condition to avoid the program entering the "exception".
Posted
Updated 19-Oct-20 21:56pm
Comments
ZurdoDev 8-Jun-17 14:16pm    
This is very, very simple. But no one can help unless you tell us which line of code is causing the error.

Your catch block isn't helping you - you're throwing away almost all of the useful details of the exception, and preventing Visual Studio from breaking when the exception is thrown.

But at a guess, the exception is probably thrown from this line:
if (dataGridView1.Rows[i].Cells[j].Value.ToString() != null)

If the Value is null, you'll get a NullReferenceException when you try to call ToString on it.

If the Value is not null, then ToString will not return null.

Remove the .ToString() call on that line.
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
    worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
else
{
    worksheet.Cells[cellRowIndex, cellColumnIndex] = String.Empty;
}
 
Share this answer
 
Comments
Member 13248805 8-Jun-17 15:02pm    
It is working after the changes!
Thank you!
if (dataGridView1.Rows[i].Cells[j].Value.ToString() != null)


the above throws exception if null found. use below step instead of above.

if ( (dataGridView1.Rows[i].Cells[j].Value ?? "").ToString() != "")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900