0

How can I check if item object is null? I have a that returns list of contacts, and I want to check if my list object is null to prevent null exception?

Dim list As New List(Of ContactU)
list = resource.ContactUs.ToList()
If list.Count <> 0 Then
    For Each item In list
        If item Then
            'Do the loop 
        End If
    Next
End If
  • 1
    What exactly is the question? The title and the body mention two different things. Checking for null in VB is `If item IsNot Nothing Then`, if my decades-old memories are not failing me. `item` would not have `.Length` or `.Count` unless `item` is a `List`... – Heretic Monkey Nov 03 '21 at 15:49
  • Does this answer your question? [How to check for a Null value in VB.NET](https://stackoverflow.com/questions/378225/how-to-check-for-a-null-value-in-vb-net) – Heretic Monkey Nov 03 '21 at 15:49
  • @HereticMonkey: in theory somewhere in that lengthy thread there _is_ the answer to the question. But there are also tons of not applicable ones. In pratice for this particular case, the answer below is correct. – Hel O'Ween Nov 03 '21 at 15:53
  • My apologies for the confusion, I will correct my questioning style in the future. I was out of words for some reason :-( –  Nov 03 '21 at 15:58
  • 1
    @HelO'Ween How is the highest voted answer a "lengthy thread"? It's short and to the point `IsNot Nothing`. And of course the answer is correct, I never said it wasn't. It's the same as the answers to the duplicate *question*, or any of the other questions this question is a duplicate of. The purpose of Stack Overflow is not to answer each individual question; it's to create a library of canonical questions and their answers so that people can find their answers fast. – Heretic Monkey Nov 03 '21 at 15:58
  • 1
    @HelO'Ween You're obviously a little new to the platform, so you may not know that the comment is posted automatically when I vote to close the question as a duplicate, and it automatically targets the question, not an answer. If the user cannot do basic research and try out answers on the question, that really isn't on me. In any case, you are free to find a more suitable duplicate -- there are many out there -- and flag the question as a duplicate as well. That would help Stack Overflow more than whining comments. – Heretic Monkey Nov 03 '21 at 16:11

1 Answers1

1

Your question title conflicts with your written question of "How can I check if item is null?" To answer that question, you check if the variable Is Nothing:

Dim list As List(Of ContactU) = resource.ContactUs.ToList()

If list Is Nothing Then
    ' Handle what happens when the list is Null/Nothing.
Else
    ' Handle what happens when the list is not Null/Nothing.
End If

When checking if a variable is not Null/Nothing, the keyword is as follows (IsNot being one keyword):

If list IsNot Nothing Then

If you want to check that a variable IsNot Nothing and in the same statement check a property of the variable, you have to use the short-circuiting version of And or Or:

If list IsNot Nothing AndAlso list.Count > 0 Then

This way, if list is Nothing, the list.Count will not get evaluated, and will not throw a null reference exception.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
HardCode
  • 6,497
  • 4
  • 31
  • 54
  • How to check for null in VB, as one would expect, has been asked and answered a few times on Stack Overflow... – Heretic Monkey Nov 03 '21 at 15:50
  • My apologies for the confusion, I will correct my questioning style in the future. I was out of words for some reason :-( But this code helped me solve my issue. Many thanks :-) –  Nov 03 '21 at 15:59
  • 1
    You could also use a [null-conditional operator](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/null-conditional-operators): `If list?.Count > 0 Then`. – Andrew Morton Nov 03 '21 at 16:46