神秘的NullReferenceException [英] Mysterious NullReferenceException

查看:60
本文介绍了神秘的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用System.Xml和XML文件的程序。我的项目中有以下代码返回NullReferenceException:


profileDataDoc = new XmlDocument();

profileDataDoc。加载(fileName);


XmlElement root = profileDataDoc.DocumentElement;

XmlNode GoalNode = root.SelectSingleNode(" / Profile_Data / Profile

[ProfileName = Spencer] / GoalWeight");

MessageBox.Show(GoalNode.Value); < ---- NullReferenceException


调试器说对象引用未设置为

对象的实例在MessageBox.Show行上。我知道字符串是正确的

对xml文件。


任何人都可以帮忙解释为什么这么说? GoalNode正确地实例化了b $ b,不是吗?


再次感谢,

Spencer

-

I am working on a program that uses System.Xml and an XML file. I have
the following code in my project that returns a NullReferenceException:

profileDataDoc = new XmlDocument();
profileDataDoc.Load(fileName);

XmlElement root = profileDataDoc.DocumentElement;
XmlNode GoalNode = root.SelectSingleNode("/Profile_Data/Profile
[ProfileName=Spencer]/GoalWeight");
MessageBox.Show(GoalNode.Value); <---- NullReferenceException

The debugger says "Object reference not set to an instance of an
object" on the MessageBox.Show line. I know the string is correct
against the xml file.

Can anyone help with why it is saying this? GoalNode is properly
instantiated, isn''t it?

Thanks again,
Spencer

--

推荐答案

Spencer写道:
Spencer wrote:

我正在使用System.Xml和XML文件的程序。我的项目中有以下代码返回NullReferenceException:


profileDataDoc = new XmlDocument();

profileDataDoc。加载(fileName);


XmlElement root = profileDataDoc.DocumentElement;

XmlNode GoalNode = root.SelectSingleNode(" / Profile_Data / Profile

[ProfileName = Spencer] / GoalWeight");

MessageBox.Show(GoalNode.Value); < ---- NullReferenceException


调试器说对象引用未设置为

对象的实例在MessageBox.Show行上。我知道字符串是正确的

对xml文件。


任何人都可以帮忙解释为什么这么说? GoalNode正确地实例化了b $ b,不是吗?


再次感谢,

Spencer

-
I am working on a program that uses System.Xml and an XML file. I have
the following code in my project that returns a NullReferenceException:

profileDataDoc = new XmlDocument();
profileDataDoc.Load(fileName);

XmlElement root = profileDataDoc.DocumentElement;
XmlNode GoalNode = root.SelectSingleNode("/Profile_Data/Profile
[ProfileName=Spencer]/GoalWeight");
MessageBox.Show(GoalNode.Value); <---- NullReferenceException

The debugger says "Object reference not set to an instance of an
object" on the MessageBox.Show line. I know the string is correct
against the xml file.

Can anyone help with why it is saying this? GoalNode is properly
instantiated, isn''t it?

Thanks again,
Spencer

--



您好Spencer,


您是否检查过什么是null? IMO,要么GoalNode为null,要么

GoalNode.Value为null。您可以使用调试器并将鼠标悬停在两个部分的

上,看看哪个是null,如果它是前者...你有一个

你的root实例化问题.SelectSingleNode,如果它是后者,

试试这个:


///

MessageBox.Show(GoalNode.Value == null?string.Empty:GoalNode.Value);

///


-

希望这会有所帮助,

Tom Spink

Hi Spencer,

Have you checked to see what is null? IMO, either GoalNode is null, or
GoalNode.Value is null. You can use the debugger and hover your mouse over
over both parts to see which is null, if it''s the former... you''ve got an
instantiation problem with your root.SelectSingleNode, if it''s the latter,
try this:

///
MessageBox.Show( GoalNode.Value == null ? string.Empty : GoalNode.Value );
///

--
Hope this helps,
Tom Spink


XmlNode GoalNode = root.SelectSingleNode(" / Profile_Data / Profile

[@ProfileName =''Spencer]''/ GoalWeight");


如果ProfileName是属性,请在其前面添加@符号。

如果引用该属性的文本值,请用单引号将其包围

marks。

Peter

-

联合创始人,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe .com

UnBlog:
http://petesbloggerama.blogspot.com



" Spencer"写道:
XmlNode GoalNode = root.SelectSingleNode("/Profile_Data/Profile
[@ProfileName=''Spencer]''/GoalWeight");

if ProfileName is an attribute, put a @ sign before it.
if referencing a text value of that attribute, surround it with single quote
marks.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Spencer" wrote:

我正在研究一个使用System.Xml和XML文件的程序。我的项目中有以下代码返回NullReferenceException:


profileDataDoc = new XmlDocument();

profileDataDoc。加载(fileName);


XmlElement root = profileDataDoc.DocumentElement;

XmlNode GoalNode = root.SelectSingleNode(" / Profile_Data / Profile

[ProfileName = Spencer] / GoalWeight");

MessageBox.Show(GoalNode.Value); < ---- NullReferenceException


调试器说对象引用未设置为

对象的实例在MessageBox.Show行上。我知道字符串是正确的

对xml文件。


任何人都可以帮忙解释为什么这么说? GoalNode正确地实例化了b $ b,不是吗?


再次感谢,

Spencer

-

I am working on a program that uses System.Xml and an XML file. I have
the following code in my project that returns a NullReferenceException:

profileDataDoc = new XmlDocument();
profileDataDoc.Load(fileName);

XmlElement root = profileDataDoc.DocumentElement;
XmlNode GoalNode = root.SelectSingleNode("/Profile_Data/Profile
[ProfileName=Spencer]/GoalWeight");
MessageBox.Show(GoalNode.Value); <---- NullReferenceException

The debugger says "Object reference not set to an instance of an
object" on the MessageBox.Show line. I know the string is correct
against the xml file.

Can anyone help with why it is saying this? GoalNode is properly
instantiated, isn''t it?

Thanks again,
Spencer

--


更正:


XmlNode GoalNode =

root.SelectSingleNode(" / Profile_Data / Profile / [@ ProfileName =''Spencer''] / GoalWeight");


-

联合创始人,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe .com

UnBlog:
http:// petesbloggerama.blogspot.com


" Spencer"写道:
Corrected:

XmlNode GoalNode =
root.SelectSingleNode("/Profile_Data/Profile/[@ProfileName=''Spencer'']/GoalWeight");

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Spencer" wrote:

我正在研究一个使用System.Xml和XML文件的程序。我的项目中有以下代码返回NullReferenceException:


profileDataDoc = new XmlDocument();

profileDataDoc。加载(fileName);


XmlElement root = profileDataDoc.DocumentElement;

XmlNode GoalNode = root.SelectSingleNode(" / Profile_Data / Profile

[ProfileName = Spencer] / GoalWeight");

MessageBox.Show(GoalNode.Value); < ---- NullReferenceException


调试器说对象引用未设置为

对象的实例在MessageBox.Show行上。我知道字符串是正确的

对xml文件。


任何人都可以帮忙解释为什么这么说? GoalNode正确地实例化了b $ b,不是吗?


再次感谢,

Spencer

-

I am working on a program that uses System.Xml and an XML file. I have
the following code in my project that returns a NullReferenceException:

profileDataDoc = new XmlDocument();
profileDataDoc.Load(fileName);

XmlElement root = profileDataDoc.DocumentElement;
XmlNode GoalNode = root.SelectSingleNode("/Profile_Data/Profile
[ProfileName=Spencer]/GoalWeight");
MessageBox.Show(GoalNode.Value); <---- NullReferenceException

The debugger says "Object reference not set to an instance of an
object" on the MessageBox.Show line. I know the string is correct
against the xml file.

Can anyone help with why it is saying this? GoalNode is properly
instantiated, isn''t it?

Thanks again,
Spencer

--


这篇关于神秘的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆