如何解决对象引用未设置为对象的实例。 [英] How Do I Solve Object Reference Not Set To An Instance Of An Object.
问题描述
我试图遍历xmlnodes。但得到此错误对象引用未设置为对象的实例。我的主要目标是通过datgridviewcell循环它,但我不知道我怎么能进一步。我知道我的网格中的字体,日期和注释的列索引。我如何循环遍历这些列索引并能够将值解析为字符串?
我的代码
< root> < value> < comment> [Font] Bold [/ Font] [DateStamp] [/ DateStamp] [Comment] [/ Comment]
字体是我的datagridview的索引2
DateStamp是我的datagridview的索引3
评论是我的datagridview的索引4
< pre lang =c#> XmlNodeList _Nodelist = _doc.SelectNodes( / root);
foreach ( _Nodelist中的XmlNode _Xnode )
{
XmlNode _data = _Xnode.SelectSingleNode( data);
XmlNodeList _CommentNodes = _data.SelectNodes( comment);
if (_ CommentNodes!= null )
{
foreach (XmlNode节点 in _CommentNodes)
{
XmlNode _comment = node.SelectSingleNode(< span class =code-string> comment);
{
string _font = _comment [ 字体跨度>]的innerText。 // 它会在此处引发错误
string _Date = _comment [ DateStamp]。InnerText;
string _Comment = _comment [ Comment 跨度>]的innerText。
}
}
}
}
此错误是因为 _comment [Font] 为空。
检查 _comment [字体] 是不是null。
如果条件,请将放在下面
if(null!= _ comment [ 字体])
{
string _font = _comment [Font]。InnerText;
}
它肯定会起作用。 :)
如果你的评论是正确的,那么该行中的_comment [Font]必须为null。试图访问InnerText属性会导致异常。
此行后面有不需要的括号:
XmlNode _comment = node.SelectSingleNode( 评论);
这些括号是否属于if(_comment!= null),它以某种方式丢失了?
此外,在访问任何属性之前,您必须单独测试是否_comment [Font],_ comment [DateStamp]或_comment [Comment]确实存在(非空)
Am trying to loop through xmlnodes. but getting this error "Object reference not set to an instance of an object." My main goal was to loop it through the datgridviewcell but i don't know how i can take it further. I know the column indexes for font,date and comment from my grid. how can i loop through those column indexes and be able to parse the value to string?
My code
<root> <value> <comment>[Font]Bold[/Font][DateStamp][/DateStamp][Comment][/Comment]
Font is index 2 from my datagridview
DateStamp is index 3 from my datagridview
Comment is index 4 from my datagridview
XmlNodeList _Nodelist = _doc.SelectNodes("/root");
foreach(XmlNode _Xnode in _Nodelist)
{
XmlNode _data = _Xnode.SelectSingleNode("data");
XmlNodeList _CommentNodes = _data.SelectNodes("comment");
if(_CommentNodes != null)
{
foreach(XmlNode node in _CommentNodes)
{
XmlNode _comment = node.SelectSingleNode("comment");
{
string _font = _comment["Font"].InnerText; //it throws the error here
string _Date = _comment["DateStamp"].InnerText;
string _Comment = _comment["Comment"].InnerText;
}
}
}
}
This error is because of _comment["Font"] is null.
Check _comment["Font"] is not null.
Put if condition here like below
if(null!=_comment["Font"])
{
string _font = _comment["Font"].InnerText;
}
It will work for sure. :)
If your comment is correct, then _comment["Font"] in that line must be null. Trying to access the InnerText property leads to the exception.
There are unneeded brackets after this line:
XmlNode _comment = node.SelectSingleNode("comment");
Could those brackets belong to a if(_comment != null), which somehow got lost?
Also, you will have to individually test wether or not _comment["Font"], _comment["DateStamp"] or _comment["Comment"] really exist (are not null) before accessing any of their properties.
这篇关于如何解决对象引用未设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!