Answered Outlook Email: Object reference not set to an instance of an object

RedRocket86

New member
Joined
Jul 3, 2020
Messages
4
Programming Experience
1-3
Good afternoon,
I am struggling with the following piece of code.
When it reaches an email with a blank Subject or Body it throws an exception: "Object reference not set to an instance of an object"
I am really struggling with this, I have been googling all day and I'm at a bit of a dead end, any help would be really appreciated, it would help me a lot.

I've enabled Explicit and Strict and it it shows an error against the following lines:
VB.NET:
                If otkMailItems.Item(iCntr).MessageClass = otkMailItem Then

                    otkMessage = otkMailItems.Item(iCntr)

Severity Code Description Project File Line Suppression State
Error BC30574 Option Strict On disallows late binding.

Severity Code Description Project File Line Suppression State
Error BC30512 Option Strict On disallows implicit conversions from 'Object' to 'MailItem'.

Full Code
VB.NET:
        Try

            Dim otkApp As Outlook.Application = New Outlook.Application
            Dim otkMailItem = "IPM.Note"
            Dim otkNameSpace As Outlook.NameSpace = otkApp.GetNamespace("MAPI")
            Dim otkInboxFolder As Outlook.MAPIFolder = otkNameSpace.Folders("Test@Email.com").Folders("Inbox")
            Dim otkMessage As Outlook.MailItem
            Dim otkMailItems As Outlook.Items = otkInboxFolder.Items
            Dim iCntr As Integer

            otkMailItems = otkMailItems.Restrict("[Unread] = true")
            For iCntr = 1 To otkMailItems.Count

                If otkMailItems.Item(iCntr).MessageClass = otkMailItem Then

                    otkMessage = otkMailItems.Item(iCntr)

                    If otkMessage.Subject.Contains("Test1") And otkMessage.Body.Contains("Test2") And otkMessage.Body.Contains("Test3") Then
                    MsgBox("otkMessage.Subject.ToString")
                    End If
                                        
                End If
            Next

            otkApp = Nothing
            otkNameSpace = Nothing
            otkMailItems = Nothing
            otkMessage = Nothing

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 
You can't do string.Contains when string is a null reference (Nothing).
Either check for Nothing with If statement, or simpler use the ?. null-conditional operator. otkMessage.Subject?.Contains("Test1")

 
I've enabled Explicit and Strict and it it shows an error against the following lines:
If you want to use Strict you have to cast, if otkMailItems.Item(iCntr) returns Object and you want to assign it to otkMessage variable that is type Outlook.MailItem use DirectCast Operator - Visual Basic
VB.NET:
otkMessage = DirectCast(otkMailItems.Item(iCntr), Outlook.MailItem)
 
Thank you John,

I have implemented your suggestions but It's still throwing the exception when it reaches the following point:
VB.NET:
If otkMailItems.Item(iCntr).MessageClass = otkMailItem Then

It throws the exception as soon as it reaches the blank email and does not go past this statement.
 
So I've been playing around and an email that contains no Body text or Subject text doesn't appear to show as an IPM.Note class of message
I have no idea why, and no idea how to fix it or get around it, If I remove the email in question, no exception is thrown and it loops through all the emails as you would expect, as soon as it reaches the blank email then Bam! exception.
 
If you get NullreferenceException on otkMailItems.Item(iCntr).MessageClass it means Item(iCntr) return Nothing (null). So the same applies, you can't access MessageClass property on Nothing.
 
Or use .? null-conditional operator it has same meaning as the more elaborate "if something isnot nothing then use something". The operator "returns Nothing if the left-hand operand evaluates to Nothing" and Nothing converts to boolean value False.
 
Back
Top