本文介绍了datagridview上的异常(nullreferenceexception)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有人可以帮我理解我的代码问题吗?它始终是异常,如果我尝试用NULL处理单元格无关紧要。 我的捕获System.Exception错误是: 对象引用未设置为对象的实例 尝试 { worksheet = workbook.ActiveSheet; worksheet.Name = ExportedFromDatGrid; int cellRowIndex = 1 ; int cellColumnIndex = 1 ; // 遍历每一行并从每列读取值。 for ( int i = -1; i < dataGridView1.Rows.Count - 1 ; i ++) { for ( int j = 0 ; j < dataGridView1.Columns.Count; j ++) { // Excel索引从1,1开始。由于第一行将具有列标题,因此添加条件检查。 if (cellRowIndex == 1 ) { worksheet.Cells [cellRowIndex,cellColumnIndex] = dataGridView1.Columns [j] .HeaderText; } 其他 { 如果(dataGridView1.Rows [i] .Cells [j] .Value.ToString()!= null ) { worksheet.Cells [cellRowIndex,cellColumnIndex] = dataGridView1.Rows [i] .Cells [j] .Value.ToString(); } else { worksheet.Cells [cellRowIndex,cellColumnIndex] = 字符串 .Empty; } // 工作: // worksheet.Cells [cellRowIndex,cellColumnIndex] = dataGridView1.Rows [i] .Cells [j]。 Value.ToString(); } cellColumnIndex ++; } cellColumnIndex = 1 ; cellRowIndex ++; } // 获取要保存的Excel的位置和文件名来自用户。 SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = Excel文件(* .xlsx)| * .xlsx |所有文件(*。 。*)| *。*; 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(例如信息); } 最后 { excel.Quit(); workbook = null ; excel = null ; } 我尝试过: 我尝试使用IF / ELSE条件来避免程序进入异常,但没有成功。解决方案 你的 catch 块没有帮助你 - 你几乎丢弃了异常的所有有用细节,并在抛出异常时阻止Visual Studio中断。 但是在猜测中,异常可能是从这一行抛出的: if(dataGridView1.Rows [ i] .Cells [j] .Value.ToString()!= null) 如果 Value 是 null ,当你试图调用 ToString NullReferenceException c> on。 如果价值 不 null ,然后 ToString 将不会返回 null 。 删除该行的 .ToString()调用。 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; } 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"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". 解决方案 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;} 这篇关于datagridview上的异常(nullreferenceexception)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 12:23