XmlDocument class - what is the reason for System.NullReferenceException

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Hi,

Can someone explain why I get System.NullReferenceException and how
does this exception fit into the logic of XmlDocument class in .Net
framework.

I am reading an xml document, such as one shown below, using the
XmlDocument class. For some reason when I get to the
/schedule/tasks/task/comments node and try to read it using
node.FirstChild.Value I get System.NullReferenceException. I tried
using IsNothing() to check for null values, but it doesn't seem to
work. Does this behavior seem logical to you? Why should I be forced
to catch this exception if I only want to process an empty xml node?
In the example below, empty <comments> tag means there's no comments.

<schedule>
<name>Customer 1</name>
<start_date>2003-04-04</start_date>
<tasks>
<task>
<caption>Task 1</caption>
<duration>2</duration>
<comments></comments>
</task>
</tasks>
</schedule>

Thanks!

Alex
 
Alex said:
Can someone explain why I get System.NullReferenceException and how
does this exception fit into the logic of XmlDocument class in .Net
framework.

I am reading an xml document, such as one shown below, using the
XmlDocument class. For some reason when I get to the
/schedule/tasks/task/comments node and try to read it using
node.FirstChild.Value I get System.NullReferenceException. I tried
using IsNothing() to check for null values, but it doesn't seem to
work. Does this behavior seem logical to you? Why should I be forced
to catch this exception if I only want to process an empty xml node?
In the example below, empty <comments> tag means there's no comments.

Could you post a short but complete example program which demonstrates
the behaviour? It's much easier to analyse it when we can easily
reproduce it.

See http://www.pobox.com/~skeet/csharp/complete.html for what I mean.
 
That node has no attributes and no value, hence its FirstChild is null. Why
would that not be logical?

Btw you need to check node.FirstChild for null, not node.FirstChild.Value,
you can't check for the value of a node that doesn't exist.

Jerry
 
Could you post a short but complete example program which demonstrates
the behaviour? It's much easier to analyse it when we can easily
reproduce it.

Here's the xml document that the program is supposed to read:

<schedule>
<name>Customer 1</name>
<start_date>2003-04-04</start_date>
<tasks>
<task>
<caption>Task 1</caption>
<duration>2</duration>
<comments></comments>
</task>
</tasks>
</schedule>

Here's the complete program that causes System.NullReferenceException:
(I know what causes the exception (<comments /> tag). I just don't
understand what the reason behind this behavior is and how one is
supposed to "work-around"? it?)

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Xml

Module Module1

Sub Main()
Dim xmlDoc As New XmlDocument
xmlDoc.Load("c:\alex\projects\arrms\sample1.xml")

Dim rootNode, itemNode As XmlNode
rootNode = xmlDoc.ChildNodes(0)
For Each itemNode In rootNode.ChildNodes()
Select Case itemNode.Name
Case "name"
Debug.WriteLine(String.Format("{0} = {1}",
itemNode.Name, itemNode.FirstChild.Value))
Case "start_date"
Debug.WriteLine(String.Format("{0} = {1}",
itemNode.Name, itemNode.FirstChild.Value))
Case "tasks"
Dim tasksNode As XmlNode
For Each tasksNode In itemNode.ChildNodes()
Dim taskNode As XmlNode
Dim value As String
For Each taskNode In tasksNode.ChildNodes()
If IsNothing(taskNode.FirstChild.Value)
Then
value = String.Empty
Else
value = taskNode.FirstChild.Value
End If
Select Case taskNode.Name
Case "caption"
Debug.WriteLine(String.Format("{0}
= {1}", taskNode.Name, value))
Case "start_offset"
Debug.WriteLine(String.Format("{0}
= {1}", taskNode.Name, value))
Case "duration"
Debug.WriteLine(String.Format("{0}
= {1}", taskNode.Name, value))
Case "comments"
Debug.WriteLine(String.Format("{0}
= {1}", taskNode.Name, value))
End Select
Next
Next
End Select
Next
End Sub

End Module
 
comments have no children unless you are assuming the text() node is the
child, then it would have a value.

reading the InnerText of the Comments node would also return the correct
value, in this case ""

Joe Feser
 
Alex said:
Here's the xml document that the program is supposed to read:

<schedule>
<name>Customer 1</name>
<start_date>2003-04-04</start_date>
<tasks>
<task>
<caption>Task 1</caption>
<duration>2</duration>
<comments></comments>
</task>
</tasks>
</schedule>

Here's the complete program that causes System.NullReferenceException:
(I know what causes the exception (<comments /> tag). I just don't
understand what the reason behind this behavior is and how one is
supposed to "work-around"? it?)

If IsNothing(taskNode.FirstChild.Value)

tries to find the value of the first child - but the FirstChild
property returns null, as there are no children.

To avoid it, just check whether or not there *are* any children
(HasChildNodes) or use InnerText.
 
Back
Top