PC Review


Reply
Thread Tools Rate Thread

Best Way To Catch A NullException

 
 
AMDRIT
Guest
Posts: n/a
 
      11th Jun 2007
It seems to me that nodeChannel("pubDate") returns an object, and if this
object is nothing then any attempt to access it's members will fail with a
nullreference exception.

I would then:

dim pubDateFailed as boolean = true
dim sTemp as string

If nodeChannel("pubDate") is not nothing then
sTemp = nodeChannel("pubDate").innerText

if stemp is string.empty then
pubDateFailed = true
else
if isdate(pubDateFailed) then
pubDateFailed = false
else
pubDateFailed = true
end if
end if

else
pubDateFailed = true
end if

if pubDateFailed then
'throw out your exception logic
else
'populate accordingly
end if


And sense you are performing the same operation over and over again, you can
break the logic up.

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

SetControl(title, nodeChannel("title"), Color.Crimson, new Rectangle(47,
16, 53, 13), dataTypes.taString, "No title!")
...
SetControl(title, nodeChannel("pubDate"), Color.Crimson, new
Rectangle(329, 16, 200, 13), dataTypes.taString, "No publish date!")

Catch (e as exception)
'unhandled/unexpected exception logic here
finally
Dim linkFeed As New linkCreated(AddressOf CreateLink)
Me.Invoke(linkFeed, title, rssLanguage, rssDescription, rssPubDate)
end try

end sub

enum dataTypes
taString
taNumber
taDate
end enum

private sub SetControl(control as Label, name as string, node as object,
foreColor as Color, bounds as Rectangle, treatAs as dataTypes, nullValue as
string)

dim bFailed as boolean = true
dim sTemp as string

If node is not nothing then
sTemp = node.innerText

if stemp is string.empty then
bFailed = true
else

select case treatAs
case taString
bFailed = false
case taDate
if isdate(sTemp) then bFailed = false
case taNumber
is isNumeric(sTemp) then bFailed = False
end select
end if

else
bFailed = true
end if


With control
.Name = name

if bFailed then
.Text = nullValue
else
.Text = sTemp
end if

.ForeColor = forecolor
.SetBounds(bounds)
or
.SetBounds(bounds.x, bounds.y, bounds.height, bounds.width)

End With

end sub

"Terrance" <(E-Mail Removed)> wrote in message
news:6434D068-31AE-41AB-A657-(E-Mail Removed)...
> 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
 
 
 
 
Mr. Arnold
Guest
Posts: n/a
 
      11th Jun 2007

"Terrance" <(E-Mail Removed)> wrote in message
news:6245F799-24EA-4BBC-874A-(E-Mail Removed)...
> 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:
>


I don't know man. It seems to me that everything in .NET C# or VB is an
object. So, either it's an object or it's not an object.

I have read XML where the InnerText element was Null or Nothing and the
code would have thrown the NullException had I not first checked asking it.
Are you an object or not an object?

I think the proper check is if InnerText <> Nothing.

You can do this little test yourself in code that should apply to your case.

dim strt as string

strt = "help"

if strt <> Nothing then
message (" I am an object.")
endif

strt = Nothing

if strt <> Nothing then
message ("I am an object.")
else
message ("I am not an object.")
endif

The same thing should apply to the XML you're working with of either it's an
object or it's not an object. I have used that in Case and IF statements to
check the XML of being an object or not being an object.






 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      12th Jun 2007
Mr. Arnold wrote:
> I think the proper check is if InnerText <> Nothing.


No: see (O)enone's post in this thread as to why you have to use the "Is"
operator to check against Nothing.

Andrew


 
Reply With Quote
 
Mr. Arnold
Guest
Posts: n/a
 
      12th Jun 2007

"Andrew Morton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mr. Arnold wrote:
>> I think the proper check is if InnerText <> Nothing.

>
> No: see (O)enone's post in this thread as to why you have to use the "Is"
> operator to check against Nothing.
>


No, I am sorry, because I have used the Nothing keyword in both manners.

if Not InnerText is Nothing

and

If InnerText <> Nothing

They both are taking the same execution path of an object not being an
object or it is an object.

And I have tested this and it makes no difference about the above two
examples.

Nothing is a Null reference pointer, and an object is either an object with
a pointer to it or it's not an object with Null reference.

 
Reply With Quote
 
\(O\)enone
Guest
Posts: n/a
 
      12th Jun 2007
Mr. Arnold wrote:
> No, I am sorry, because I have used the Nothing keyword in both
> manners.
> if Not InnerText is Nothing
>
> and
>
> If InnerText <> Nothing
>
> They both are taking the same execution path of an object not being an
> object or it is an object.


OK, then here are two things for you to try:

1. Turn Option Strict On in your source file (which I'd highly recommend at
all times). Suddenly the "=" and "<>" versions don't compile (Option Strict
requires you to use Is or IsNot instead)

2. With Option Strict switched back off again for a moment, try the
following:

\\\
Dim f As Object = New Form

If f Is Nothing Then
MsgBox("f is nothing")
End If
If f IsNot Nothing Then
MsgBox("f is not nothing")
End If
///

Execute it and unsurprisingly it'll tell you that "f is not nothing".

Now replace the "Is" with "=" and "IsNot" with "<>". Now when you execute
the code you get a run-time error:

Operator '=' is not defined for type 'Form' and 'Nothing'.

Trust me: use "Is" and "IsNot" for comparing object references, and "=" and
"<>" for comparing values.

--

(O)enone


 
Reply With Quote
 
Mr. Arnold
Guest
Posts: n/a
 
      12th Jun 2007

"(O)enone" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Mr. Arnold wrote:
>> No, I am sorry, because I have used the Nothing keyword in both
>> manners.
>> if Not InnerText is Nothing
>>
>> and
>>
>> If InnerText <> Nothing
>>
>> They both are taking the same execution path of an object not being an
>> object or it is an object.

>
> OK, then here are two things for you to try:
>
> 1. Turn Option Strict On in your source file (which I'd highly recommend
> at all times). Suddenly the "=" and "<>" versions don't compile (Option
> Strict requires you to use Is or IsNot instead)


There was no Option Strict prior to .Net 2005. With Option Strict off, which
is the default, this is really not applicable.

>
> 2. With Option Strict switched back off again for a moment, try the
> following:
>
> \\\
> Dim f As Object = New Form
>
> If f Is Nothing Then
> MsgBox("f is nothing")
> End If
> If f IsNot Nothing Then
> MsgBox("f is not nothing")
> End If
> ///
>
> Execute it and unsurprisingly it'll tell you that "f is not nothing".
>
> Now replace the "Is" with "=" and "IsNot" with "<>". Now when you execute
> the code you get a run-time error:
>
> Operator '=' is not defined for type 'Form' and 'Nothing'.
>
> Trust me: use "Is" and "IsNot" for comparing object references, and "="
> and "<>" for comparing values.


I appreciate your comments and I'll be aware of this, if I need Option
Strict ever enabled. I guess this has to be the reason IsNothing()
disappeared out of VB.NET 2.0


 
Reply With Quote
 
\(O\)enone
Guest
Posts: n/a
 
      12th Jun 2007
Mr. Arnold wrote:
>> 1. Turn Option Strict On in your source file


> There was no Option Strict prior to .Net 2005. With Option Strict
> off, which is the default, this is really not applicable.


There definitely was in VS2003, and I'm pretty sure it was there in VS2002
too. Switching it on can save you from all sorts of errors that you
otherwise wouldn't spot until your code happens to hit a specific set of
conditions at run-time. And it might be your users that hit those conditions
before you do.

Personally I'd never write any code with Option Strict switched off. Up to
you though.

> I appreciate your comments and I'll be aware of this, if I need Option
> Strict ever enabled. I guess this has to be the reason IsNothing()
> disappeared out of VB.NET 2.0


IsNothing is still there in VB.NET 2.0, in the
Microsoft.VisualBasic.Information namespace. (You'll obviously need a
reference to Microsoft.VisualBasic before you can use it, though). To me,
the syntax "If IsNothing(variable) Then" is much clumsier than "Is variable
Is Nothing Then" but again it's up to you.

HTH,

--

(O)enone



 
Reply With Quote
 
Mr. Arnold
Guest
Posts: n/a
 
      12th Jun 2007

"(O)enone" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mr. Arnold wrote:
>>> 1. Turn Option Strict On in your source file

>
>> There was no Option Strict prior to .Net 2005. With Option Strict
>> off, which is the default, this is really not applicable.

>
> There definitely was in VS2003, and I'm pretty sure it was there in VS2002
> too. Switching it on can save you from all sorts of errors that you
> otherwise wouldn't spot until your code happens to hit a specific set of
> conditions at run-time. And it might be your users that hit those
> conditions before you do.


I'll take your word for it. I never had to us it.


>
> Personally I'd never write any code with Option Strict switched off. Up to
> you though.



I'll keep it mind, the next time on a VB.net project.

>
>> I appreciate your comments and I'll be aware of this, if I need Option
>> Strict ever enabled. I guess this has to be the reason IsNothing()
>> disappeared out of VB.NET 2.0

>
> IsNothing is still there in VB.NET 2.0, in the
> Microsoft.VisualBasic.Information namespace. (You'll obviously need a
> reference to Microsoft.VisualBasic before you can use it, though). To me,
> the syntax "If IsNothing(variable) Then" is much clumsier than "Is
> variable Is Nothing Then" but again it's up to you.


I have used the Microsoft.VisualBasic namespace, but not for this.

IsNothing() or Is Nothing as long as they work is the bottom line. I include
the <> in there as well as long Option Strict is off.

I came from a VB 6 background but have spent most of my .NET development in
C#, no Options.


 
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 03:35 PM
Array nullexception latin & geek via DotNetMonster.com Microsoft VB .NET 5 20th Apr 2007 06:30 PM
List View variable throwing NullException Miesha.James@gmail.com Microsoft VC .NET 2 29th Mar 2007 03:38 AM
NullException on Exit app when performing updates on the dataset kuhrty Microsoft ADO .NET 0 20th Jul 2006 08:48 PM
Catch block is failing to catch exceptions when not run from MSDev CodeSlayer Microsoft C# .NET 2 16th Feb 2006 06:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:25 AM.