出错请高人指点。用户代码未处理nullreferenceexception未将对象引用设置到对象的实例,

opelope 2016-03-21 02:35:00
出错请高人指点,以下代码编译能过,运行报错。“用户代码未处理nullreferenceexception未将对象引用设置到对象的实例”



private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{

int intCurrentRowNumber = this.dataGridView1.CurrentCell.RowIndex;
string aa = this.dataGridView1.Rows[intCurrentRowNumber].Cells[0].Value.ToString();

}
...全文
126 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Magnetism_net 2016-03-21
  • 打赏
  • 举报
回复
this.dataGridView1.Rows[intCurrentRowNumber].Cells[0].Value.ToString(); 这个地方Rows[intCurrentRowNumber]可能就是得到一个null对象,那么你后续的.Cells[0]就会nullreferenceexception 并且随之而来的Cells[0]里面也可能是一个null值,你去.Value也会报这个错 如果Value本身也是个null值,那么你ToString也会报这个错。 你这一句话里面到处是坑呐。 你必须对Rows[intCurrentRowNumber] Cells[0] Value 都做null值判断 或者这么写 string aa = this.dataGridView1.Rows[intCurrentRowNumber]?.Cells[0]?.Value?.ToString(); 不过需要vs2015或者C# 6.0支持

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧