Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null check in VB

All I want to do is check if an object is null, but no matter what I do, if it compiles, it throws a NullReferenceException just trying to check! Here's what I've done:

    If ((Not (comp.Container Is Nothing)) And (Not (comp.Container.Components Is Nothing))) Then         For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1             fixUIIn(comp.Container.Components.Item(i), style)         Next     End If      If ((Not IsDBNull(comp.Container)) And (Not IsDBNull(comp.Container.Components))) Then         For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1             fixUIIn(comp.Container.Components.Item(i), style)         Next     End If      If ((Not IsNothing(comp.Container)) And (Not IsNothing(comp.Container.Components))) Then         For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1             fixUIIn(comp.Container.Components.Item(i), style)         Next     End If      If ((Not (comp.Container Is DBNull.Value)) And (Not (comp.Container.Components Is DBNull.Value))) Then         For i As Integer = 0 To comp.Container.Components.Count() Step 1             fixUIIn(comp.Container.Components.Item(i), style)         Next     End If 

I've looked through VB books, searched several forums, and everything that SHOULD work doesn't! Sorry for asking such a remedial question, but I just need to know.

Just so you know, the debugger says that the null object is comp.Container

like image 811
Ky. Avatar asked Apr 07 '11 14:04

Ky.


People also ask

How do you check if a value is null in VB?

Use the IsNull function to determine whether an expression contains a Null value. Expressions that you might expect to evaluate to True under some circumstances, such as If Var = Null and If Var <> Null , are always False. This is because any expression containing a Null is itself Null and therefore False.

What is the null check?

A null indicates that a variable doesn't point to any object and holds no value. You can use a basic 'if' statement to check a null in a piece of code. Null is commonly used to denote or verify the non-existence of something.

How do you write null in VB?

When checking whether a reference (or nullable value type) variable is null , do not use = Nothing or <> Nothing . Always use Is Nothing or IsNot Nothing . For strings in Visual Basic, the empty string equals Nothing . Therefore, "" = Nothing is true.


2 Answers

Change your Ands to AndAlsos

A standard And will test both expressions. If comp.Container is Nothing, then the second expression will raise a NullReferenceException because you're accessing a property on a null object.

AndAlso will short-circuit the logical evaluation. If comp.Container is Nothing, then the 2nd expression will not be evaluated.

like image 106
Ken Pespisa Avatar answered Sep 22 '22 16:09

Ken Pespisa


Your code is way more cluttered than necessary.

Replace (Not (X Is Nothing)) with X IsNot Nothing and omit the outer parentheses:

If comp.Container IsNot Nothing AndAlso comp.Container.Components IsNot Nothing Then     For i As Integer = 0 To comp.Container.Components.Count() - 1         fixUIIn(comp.Container.Components(i), style)     Next End If 

Much more readable. … Also notice that I’ve removed the redundant Step 1 and the probably redundant .Item.

But (as pointed out in the comments), index-based loops are out of vogue anyway. Don’t use them unless you absolutely have to. Use For Each instead:

If comp.Container IsNot Nothing AndAlso comp.Container.Components IsNot Nothing Then     For Each component In comp.Container.Components         fixUIIn(component, style)     Next End If 
like image 34
Konrad Rudolph Avatar answered Sep 21 '22 16:09

Konrad Rudolph