PC Review


Reply
Thread Tools Rate Thread

Best Way To Catch A NullException

 
 
=?Utf-8?B?VGVycmFuY2U=?=
Guest
Posts: n/a
 
      11th Jun 2007
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.
--
TC
 
Reply With Quote
 
 
 
 
Andrew Morton
Guest
Posts: n/a
 
      11th Jun 2007
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.


Surely it's

If nodeChannel("pubDate").InnerText Is Nothing Then ....

?

Andrew


 
Reply With Quote
 
=?Utf-8?B?VGVycmFuY2U=?=
Guest
Posts: n/a
 
      11th Jun 2007
The reason why the syntax:
if nodeChannel("pubDate").Innertext = Nothing

is not working is because the object isn't set to anything. Therefore, if
the object is nothing how can you compare Nothing to Nothing? Is there an
alternative?
--
TC


"Andrew Morton" wrote:

> 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.

>
> Surely it's
>
> If nodeChannel("pubDate").InnerText Is Nothing Then ....
>
> ?
>
> Andrew
>
>
>

 
Reply With Quote
 
=?Utf-8?B?TWFuaXNoIEJhZm5h?=
Guest
Posts: n/a
 
      11th Jun 2007
Hi,
you can also try something like this:
if node.InnerText.Length=0
node.InnerText = "blah blah";
else
node.InnerText ="Actual Text"

--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



"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.
> --
> TC

 
Reply With Quote
 
=?Utf-8?B?VGVycmFuY2U=?=
Guest
Posts: n/a
 
      11th Jun 2007
By the way the code
if nodeChannel("pubDate") is Nothing then...
does not work either. I tried this already before.
--
TC


"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.
> --
> TC

 
Reply With Quote
 
DArnold
Guest
Posts: n/a
 
      11th Jun 2007

If it were me, I would be doing a null check first an avoid the
situation of the exception.

bBad = false

If nodeChannel("description").InnerText <> Nothing then
.Text = nodeChannel("description").InnerText
else
.Text = vbNullString
bBad = true
end if


if bBad then
message 'and whatever else you have to do
endif
 
Reply With Quote
 
\(O\)enone
Guest
Posts: n/a
 
      11th Jun 2007
Terrance wrote:
> The reason why the syntax:
> if nodeChannel("pubDate").Innertext = Nothing
>
> is not working is because the object isn't set to anything.
> Therefore, if the object is nothing how can you compare Nothing to
> Nothing?


By using the Is operator, as Andrew already said.

Internally an object that contains Nothing is actually a pointer with a
reference to NULL (zero). The "Is" operator allows you to compare two object
pointers. So if you have two form variables, for example, you could tell
whether they point to the same object instance (not two instances of the
same form but the actual same instance) by using the code:

\\\
If MyForm1 Is MyForm2 Then
...
End If
///

Note that this is not the same is using the = operator, which compares
values rather than references. Similarly you can use IsNot in VB2005 to
determine whether two object references are not the same instance, as
opposed to <> for values. (If you're using VS2003 or earlier, you need to
use the slightly more clumsy "If Not a Is b Then" syntax).

You can use this to determine whether an object set to Nothing (which has a
pointer to NULL) is Nothing (which also has a pointer to NULL) in the same
way.

Try putting this into your code prior to the line where your error occurs:

\\\
If nodeChannel Is Nothing Then
MsgBox("nodeChannel Is Nothing")
ElseIf nodeChannel("pubDate") Is Nothing Then
MsgBox("nodeChannel(""pubDate"") Is Nothing")
ElseIf nodeChannel("pubDate").InnerText Is Nothing Then
MsgBox("nodeChannel(""pubDate"".InnerText) Is Nothing")
Else
MsgBox("Type of InnerText is " &
TypeName(nodeChannel("pubDate").InnerText))
End If
///

Run that and see what it produces. (This assumes you're running in a
WinForms environment, if you're not you'll need to replace the MsgBox calls
with Debug.WriteLine or something else.)

--

(O)enone


 
Reply With Quote
 
=?Utf-8?B?VGVycmFuY2U=?=
Guest
Posts: n/a
 
      11th Jun 2007
Thanks everyone for your input. I decided to look for the child node and if I
find the pubDate element I set a boolean variable to true; If I don't find
the pubDate element I set the boolean variable to false. I pass the boolean
value to a Sub routine and check whether it's true or false. If it's true I
process as normal if it's false I throw the execption which works for me. I
appreciate the everyone's comments and help.

The issue that I found was that the object didn't exist when I read an xml
file that didn't have the element which in turn makes it hard to compare
against the NOTHING value. For Example:

If I had a variable
Dim a as String
Then I tried to do the following:
if a.length = 0 then... this would cause the nullreferenceexecption because
a was never initialized. Well, my problem was similiar except I was using
elements straight from a xml file and if the element didn't exist then I
would get a runtime error.
--
TC


"DArnold" wrote:

>
> If it were me, I would be doing a null check first an avoid the
> situation of the exception.
>
> bBad = false
>
> If nodeChannel("description").InnerText <> Nothing then
> .Text = nodeChannel("description").InnerText
> else
> .Text = vbNullString
> bBad = true
> end if
>
>
> if bBad then
> message 'and whatever else you have to do
> endif
>

 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      11th Jun 2007
Terrance wrote:
> The reason why the syntax:
> if nodeChannel("pubDate").Innertext = Nothing
>
> is not working is because the object isn't set to anything.
> Therefore, if the object is nothing how can you compare Nothing to
> Nothing? Is there an alternative?


You don't use the = operator when checking for Nothing, you use the Is
operator.

However, are you saying that nodeChannel("pubDate") is the entity which is
nothing, or is it nodeChannel("pubDate").Innertext?

If the former, then what data type is nodeChannel and does that type have a
method for determining if a particular value of it exists?

Andrew


 
Reply With Quote
 
Kelly Ethridge
Guest
Posts: n/a
 
      11th Jun 2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
try..catch catch the error only running inside of VS.NET Dragos Hilbert Microsoft C# .NET 2 11th Mar 2008 04:35 PM
Array nullexception latin & geek via DotNetMonster.com Microsoft VB .NET 5 20th Apr 2007 07:30 PM
List View variable throwing NullException Miesha.James@gmail.com Microsoft VC .NET 2 29th Mar 2007 04:38 AM
NullException on Exit app when performing updates on the dataset kuhrty Microsoft ADO .NET 0 20th Jul 2006 09:48 PM
Catch block is failing to catch exceptions when not run from MSDev CodeSlayer Microsoft C# .NET 2 16th Feb 2006 07:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:56 AM.