Mysterious NullReferenceException

  • Thread starter Thread starter Spencer
  • Start date Start date
S

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 said:
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

--

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 );
///
 
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
 
Corrected:

XmlNode GoalNode =
root.SelectSingleNode("/Profile_Data/Profile/[@ProfileName='Spencer']/GoalWeight");
 
Neither node is null after running the debugger, and the ProfileName is
an element, not an attribute. There doesn't appear to be anything wrong
with the code, it just doesn't compile!

Spencer


Tom said:
Spencer said:
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

--

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 ); ///



--
 
Also, the error disappears when I add single quotations around the
element name after ProfileName=, but the string returned by
SelectSingleNode is ALWAYS zero-length, regardless of what I do or what
the XML file contains. It does this in two completely separate
programs, using different XML and source files.

Spencer


Tom said:
Spencer said:
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

--

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 ); ///



--
 
Spencer said:
Neither node is null after running the debugger, and the ProfileName is
an element, not an attribute. There doesn't appear to be anything wrong
with the code, it just doesn't compile!

It apparently *does* compile, otherwise you couldn't be running it to
get an exception.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
I have corrected this part of the problem. The problem was that I
wasn't putting single quotes around the content of the element
"ProfileName". I was putting [ProfileName=Example] when I should have
been putting [ProfileName='Example']

However, this brings up another problem that does not involve an
exception. The application is too large to post here, but the problem
is isolated to the following lines:


--------
profileDataDoc = new XmlDocument();
profileDataDoc.Load("OutData.xml");

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


The value GoalNode.Value is ALWAYS zero-length, as if the element
contains no data, which it does. The message box displays with no text,
but no NullReferenceException is raised because the Value is not null.
This problem has developed in two separate applications. There must be
something I'm repeating. I am absolutely sure that the XML file is
correct.

Thank you for your assistance.

Spencer

It apparently does compile, otherwise you couldn't be running it to
get an exception.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.



--
 
Spencer said:
I have corrected this part of the problem. The problem was that I
wasn't putting single quotes around the content of the element
"ProfileName". I was putting [ProfileName=Example] when I should have
been putting [ProfileName='Example']
Good.

However, this brings up another problem that does not involve an
exception. The application is too large to post here, but the problem
is isolated to the following lines:

If you've isolated the problem to those lines, you should be able to
construct a short but complete program which demonstrates the problem.
--------
profileDataDoc = new XmlDocument();
profileDataDoc.Load("OutData.xml");

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

The value GoalNode.Value is ALWAYS zero-length, as if the element
contains no data, which it does. The message box displays with no text,
but no NullReferenceException is raised because the Value is not null.

It wouldn't anyway. It would show an empty message box. How sure are
you that Value really isn't null?
This problem has developed in two separate applications. There must be
something I'm repeating. I am absolutely sure that the XML file is
correct.

If GoalWeight is an element, then Value will in fact be null.

Again, a short but complete program would help to speed up the
diagnostic process.
 
I have duplicated the problem in a simple console test program and XML
file. The program compiles and runs fine, but the Value property is
still empty. The code for the program is as follows:

<--------------->
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace TestProgram
{
class Program
{
static void Main(string[] args)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load("G:\\Visual Studio
2005\\Projects\\TestProgram\\TestProgram\\TestXmlFile.xml");

XmlElement root = xdoc.DocumentElement;
XmlNode childNode =
root.SelectSingleNode("/TEST/TestParentValue/TestChildValue");

Console.WriteLine(childNode.Value); //Zero-length writes
}
}
}
<--------------->

(I apologize for the formatting)

And here is the test XML file:

<--------------->
<?xml version="1.0" encoding="utf-8" ?>
<TEST>
<TestParentValue>
<TestChildValue>DATA</TestChildValue>
</TestParentValue>
</TEST>
<--------------->


When the program runs, it should output "DATA", but, as usual, it
returns absolutely nothing. I know the childNode.Value is not null
because I do not receive a NullReferenceException like I did before.
The string is simply zero-length and I don't know why.

Spencer

Spencer said:
I have corrected this part of the problem. The problem was that I
wasn't putting single quotes around the content of the element
"ProfileName". I was putting [ProfileName=Example] when I should
have been putting [ProfileName='Example']
Good.

However, this brings up another problem that does not involve an
exception. The application is too large to post here, but the
problem is isolated to the following lines:

If you've isolated the problem to those lines, you should be able to
construct a short but complete program which demonstrates the problem.
--------
profileDataDoc = new XmlDocument();
profileDataDoc.Load("OutData.xml");

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

The value GoalNode.Value is ALWAYS zero-length, as if the element
contains no data, which it does. The message box displays with no
text, but no NullReferenceException is raised because the Value is
not null.

It wouldn't anyway. It would show an empty message box. How sure are
you that Value really isn't null?
This problem has developed in two separate applications. There must
be something I'm repeating. I am absolutely sure that the XML file
is correct.

If GoalWeight is an element, then Value will in fact be null.

Again, a short but complete program would help to speed up the
diagnostic process.



--
 
Spencer said:
I have duplicated the problem in a simple console test program and XML
file. The program compiles and runs fine, but the Value property is
still empty. The code for the program is as follows:

Hi Spencer,

Change 'Value' to 'InnerText':

///
Console.WriteLine( childNode.InnerText );
///
 
Spencer said:
I have duplicated the problem in a simple console test program and XML
file. The program compiles and runs fine, but the Value property is
still empty. The code for the program is as follows:

(I apologize for the formatting)

No problem - that was exactly what I needed, along with the XML file.
When the program runs, it should output "DATA", but, as usual, it
returns absolutely nothing.

Nope, it should print out nothing, because you're using the Value
property of an XmlElement. Look up XmlNode.Value in MSDN - you'll see
that for an element, the Value property returns null. Now, your XPath
expression finds an element, so it would make sense for it to return
null.
I know the childNode.Value is not null
because I do not receive a NullReferenceException like I did before.

No, that just shows that childNode isn't null. You were getting an
exception before because the *node* variable was null, not because its
Value property was null.

You can write Console.WriteLine (null); without getting a
NullReferenceException, and that's effectively exactly what you've got
here.

To confirm whether or not that's the case, try this:

Console.WriteLine(childNode.Value==null);

Fairly obviously, I've done that - and unsurprisingly, it prints
"True".
The string is simply zero-length and I don't know why.

It's null for the reasons given above. To get the text you want, use
the InnerText property instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top