473,480 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Mysterious NullReferenceException

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

--

Jul 3 '06 #1
11 5141
Spencer wrote:
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 );
///

--
Hope this helps,
Tom Spink
Jul 3 '06 #2
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:
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

--

Jul 3 '06 #3
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:
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

--

Jul 3 '06 #4
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 Spink wrote:
Spencer wrote:
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 ); ///


--

Jul 4 '06 #5
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 Spink wrote:
Spencer wrote:
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 ); ///


--

Jul 4 '06 #6
Spencer <se**************@yahoo.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 4 '06 #7
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
Jon Skeet [C# MVP] wrote:
Spencer <se**************@yahoo.comwrote:
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.


--

Jul 4 '06 #8
Spencer <se**************@yahoo.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 4 '06 #9
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\\TestXmlF ile.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
Jon Skeet [C# MVP] wrote:
Spencer <se**************@yahoo.comwrote:
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.


--

Jul 4 '06 #10
Spencer wrote:
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 );
///

--
Hope this helps,
Tom Spink
Jul 4 '06 #11
Spencer <se**************@yahoo.comwrote:
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:
<snip - thanks>
(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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 5 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9078
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
5
2618
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
5
3040
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
2
3173
by: Enrico Pangan | last post by:
I'm trying to call some functions in a C++ Dll, "Library.dll" from C#. Some functions work but some return the NullReferenceException. I have here the source code for the C++ version and for the...
2
7767
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
4079
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
13
2409
by: Max | last post by:
Hi There! I'm having a mysterious error right after I login using Forms Authentication in my ASP.NET app. Below is the error... Exception Details: System.NullReferenceException: Object...
6
22191
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
9
351
by: Xero | last post by:
could anybody describes a scenario where this error will occur? i have visited the page about the NullReferenceException in msdn library but i still have no idea what it is about. thanks. ...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.