I think everyone is missing where your exception is being created from.
They seem to be assuming that the return value of the InnerText is
causing it. However, I will assume that you are trying to use an
XmlElement object returned from an XmlDocument (from what I can tell),
and directly calling the InnerText on that. I would create a simple
factory method to initialize each label with passed in arguments,
something similar to this:
Private Sub CreateControls()
title = CreateLabel("lblMyTitle", "title", "No title", 47, 16,
53, 13)
rssLanguage = CreateLabel("lblRssLanguage", "language", "No
language", 194, 16, 39, 13)
rssDescription = CreateLabel("lblRssDescription",
"description", "No description", 75, 55, 500, 13)
rssPubDate = CreateLabel("lblPubDate", "pubDate", "No publish
date!", 329, 16, 200, 13)
Dim linkFeed As New linkCreated(AddressOf CreateLink)
Me.Invoke(linkFeed, title, rssLanguage, rssDescription, rssPubDate)
End Sub
Private Function CreateLabel(ByVal name As String, ByVal nodeName As
String, ByVal errMsg As String, ByVal x As Integer, ByVal y As Integer,
ByVal w As Integer, ByVal h As Integer) As Label
Dim item As New System.Windows.Forms.Label
With item
.Name = name
.ForeColor = Color.Crimson
.SetBounds(x, y, w, h)
Dim node As XmlElement = nodeChannel(nodeName)
If node IsNot Nothing Then
.Text = node.InnerText
Else
.Text = errMsg
End If
End With
Return item
End Function
End Class
Kelly
Terrance wrote:
> Hello, I was wondering if someone can give me a few pointers in catching an
> exception correctly or at least making the code a little more elegant than
> what I have. I have a rss reader that I built in VB. When I read the contents
> from the xml file I create several labels controls that I place on the form
> at runtime. My problem is that some rss feeds do not have a particular node
> (in this case a "pubDate" for the publishing date of the feed) which causes
> the application to throw a NullReferenceException. I've tried the following
> code:
>
> if nodeChannel("pubDate").InnerText = Nothing then ....
>
> Unfortunately, this doesn't work when it comes to nulls. So, I currently
> have the following code but I'm not happy with it:
>
> Private Sub CreateControls()
> title = New System.Windows.Forms.Label
> rssLanguage = New System.Windows.Forms.Label
> rssDescription = New System.Windows.Forms.Label
> rssPubDate = New System.Windows.Forms.Label
>
> Try
> With title
> .Name = "lblMyTitle"
> .Text = nodeChannel("title").InnerText
> .ForeColor = Color.Crimson
> .SetBounds(47, 16, 53, 13)
> End With
>
> With rssLanguage
> .Name = "lblRssLanguage"
> .Text = nodeChannel("language").InnerText
> .ForeColor = Color.Crimson
> .SetBounds(194, 16, 39, 13)
> End With
>
> With rssDescription
> .Name = "lblRssDescription"
> .Text = nodeChannel("description").InnerText
> .ForeColor = Color.Crimson
> .SetBounds(75, 55, 500, 13)
> End With
>
> With rssPubDate
> .Name = "lblPubDate"
> .Text = nodeChannel("pubDate").InnerText
> .ForeColor = Color.Crimson
> .SetBounds(329, 16, 200, 13)
> End With
>
> Catch e As NullReferenceException
> With rssPubDate
> .Name = "lblPubDate"
> .Text = "No publish date!"
> .ForeColor = Color.Crimson
> .SetBounds(329, 16, 200, 13)
> End With
> Finally
> Dim linkFeed As New linkCreated(AddressOf CreateLink)
> Me.Invoke(linkFeed, title, rssLanguage, rssDescription,
> rssPubDate)
> End Try
> End Sub
>
> As you can see when the exception occurs I'm assuming there's no pubDate in
> the rss feed so I create a label saying No Publish Date. Is there a better
> way in handling this. If so, please share you thoughts and ideas. I would
> love to learn your methodology when you encounter issues such as mine. Thanks.