C#windows应用程序,“未将对象引用设置到对象的实例。”求助

shamohai5566 2010-01-25 08:52:45
我的程序代码如下:
baseclass.Userclass uc = new testCfor2.baseclass.Userclass();
string strOperater = uc.UsingName(strUserName);
string strTime = Convert.ToString(System.DateTime.Now);
for (int i = 0; i < j-1; i++)
{
//int a;
string IsGood="";
IsGood =this.dataGridView1.Rows[i].Cells[7].Value .ToString ();
if (IsGood .Equals("合格"))
{
int strReordId = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value);

string strYorN = "合格";

string strDitail;
strDitail = this.dataGridView1.Rows[i].Cells[8].Value.ToString ();
baseclass.Checkclass chcalss = new testCfor2.baseclass.Checkclass();
chcalss.CheckRecordAdd(strReordId, strOperater, strYorN, strTime, strDitail);


}
if (IsGood.Equals("不合格"))
{
int strReordId = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value);

string strYorN = "不合格";

string strDitail="";
strDitail= this.dataGridView1.Rows[i].Cells[8].Value.ToString().Trim ();
baseclass.Checkclass chcalss = new testCfor2.baseclass.Checkclass();
chcalss.CheckRecordAdd(strReordId, strOperater, strYorN, strTime, strDitail);

}

}
调试运行后,弹出对话框提示错误,“未将对象引用设置到对象的实例。”“未处理NullReferenceException”
请问高手,这个问题怎么解决,大家帮忙,我是新手,谢谢!
...全文
344 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
xray2005 2010-01-25
  • 打赏
  • 举报
回复
.Rows[i].Cells[8].Value//Rows[i]是否为NULL?Cells[8]是否为NULL?value是否为NULL???

如果是NULL,那么你ToString()这些都不对了。

加判断吧。

可以是“”或者String.Empty字符串。
WTPMCheng 2010-01-25
  • 打赏
  • 举报
回复
if(Convert.ToString(this.dataGridView1.Rows[i].Cells[8].Value.ToString())!="")
{
//判断value值是否为""
}

或者是
if(String.IsNullOrEmperty(this.dataGridView1.Rows[i].Cells[8].Value.ToString())==true)
{
==true 就为null或者是""
}
-汪帆- 2010-01-25
  • 打赏
  • 举报
回复
试试
string strDitail = string.Empty;
strDitail = this.dataGridView1.Rows[i].Cells[8].Value.ToString ();
灵雨飘零 2010-01-25
  • 打赏
  • 举报
回复
.Rows[i].Cells[8].Value//Rows[i]是否为空?Cells[8]是否为空?value是否为空???
shamohai5566 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 holydie 的回复:]
用dataGridView1.Rows[i].Cells.count 判断一下吧
if( 》=8)
strDitail = this.dataGridView1.Rows[i].Cells[8].Value.ToString ();
if( <8)
strDitail ="";//就是你说的“空字符串”

[/Quote]
大哥,还是不行呀!如果单元格为空,这个Count也计数吧?总之找你说的做了,还是报错。怎么判断一个单元格是不是空呀?
Kobayashi 2010-01-25
  • 打赏
  • 举报
回复
strDitail = this.dataGridView1.Rows[i].Cells[8].Value.ToString ();
这里的8最好写成这列的列的名称。比如:
this.dataGridView1.Rows[i].Cells["CellName"].Value.ToString ();
wangbaiwu 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ptpa 的回复:]
有cells8吗
[/Quote]

up
holydie 2010-01-25
  • 打赏
  • 举报
回复
用dataGridView1.Rows[i].Cells.count 判断一下吧
if( 》=8)
strDitail = this.dataGridView1.Rows[i].Cells[8].Value.ToString ();
if(<8)
strDitail ="";//就是你说的“空字符串”
ztenv 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shamohai5566 的回复:]
引用 4 楼 haliput 的回复:
引用 3 楼 shamohai5566 的回复:
对,是的,是空值!空不行吗?必须要有字符串吗 ?

自己加判断呀。

如果是空,我输入“”空字符串可以吗?谢谢
[/Quote]
要么当为空的时候你输入空的字符串(空值和空的字符串是不同的概念),要么用try...catch...忽略空值异常(不是很好的解决办法,但也能解决这个问题)
mngzilin 2010-01-25
  • 打赏
  • 举报
回复
什么问题?
流氓兔 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shamohai5566 的回复:]
引用 4 楼 haliput 的回复:
引用 3 楼 shamohai5566 的回复:
对,是的,是空值!空不行吗?必须要有字符串吗 ?

自己加判断呀。

如果是空,我输入“”空字符串可以吗?谢谢
[/Quote]
如果是NULL,那就不能使用ToString(),那样肯定报错的啊!
是NULL,就设置为“”
  • 打赏
  • 举报
回复
如果不影响功能,就try { }
catch { }
个空的,不让报错出来就Over了!
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shamohai5566 的回复:]
引用 4 楼 haliput 的回复:
引用 3 楼 shamohai5566 的回复:
对,是的,是空值!空不行吗?必须要有字符串吗 ?

自己加判断呀。

如果是空,我输入“”空字符串可以吗?谢谢
[/Quote]
你在编辑列里从0往前数,看看你的列号是不是弄错了,包括你Visbile为FALSE的列!
还有,你如果是要对单行进行操作可以用这段代码: string strInf = this.dataGridView1["你的列号名", this.dataGridView1.CurrentRow.Index].Value.ToString();
dapsxianfeng2010 2010-01-25
  • 打赏
  • 举报
回复
这么多人回答,那我顶了
shamohai5566 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 haliput 的回复:]
引用 3 楼 shamohai5566 的回复:
对,是的,是空值!空不行吗?必须要有字符串吗 ?

自己加判断呀。
[/Quote]
如果是空,我输入“”空字符串可以吗?谢谢
ptpa 2010-01-25
  • 打赏
  • 举报
回复
有cells8吗
liaoyukun111 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lianshaohua 的回复:]
.Rows[i].Cells[8].Value//Rows[i]是否为空?Cells[8]是否为空?value是否为空???
[/Quote]

UP
王子样 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shamohai5566 的回复:]
对,是的,是空值!空不行吗?必须要有字符串吗 ?
[/Quote]
自己加判断呀。
shamohai5566 2010-01-25
  • 打赏
  • 举报
回复
对,是的,是空值!空不行吗?必须要有字符串吗 ?
lerit 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lianshaohua 的回复:]
.Rows[i].Cells[8].Value//Rows[i]是否为空?Cells[8]是否为空?value是否为空???
[/Quote]
没错!
加载更多回复(3)

110,566

社区成员

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

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

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