This project is read-only.

Error Nullable object have to have value

Mar 4, 2014 at 7:32 AM
Edited Mar 4, 2014 at 7:32 AM
Hello,

i have this code:
                        imapClient.Folders.Inbox.Messages.Download("ALL", Enums.MessageFetchMode.None + Enums.MessageFetchMode.InternalDate, -1)
                        For Each mx In imapClient.Folders.Inbox.Messages
                            Try
                                Dim soubor As String = path & mx.InternalDate.Value.ToString("yyyyMMdd-HHmmss") & "-" & mx.UId.ToString & ".eml"
                                Console.WriteLine(soubor)
                                If Not File.Exists(soubor) Then
                                    Dim eml As String = mx.DownloadRawMessage()
                                    File.WriteAllText(soubor, eml)
                                End If
                            Catch ex As Exception
                                Console.WriteLine(ex.Message)
                                File.AppendAllText("imapArchive.log", Now.ToString("yyyyMMdd-HHmmss ") & ex.Message & vbCrLf)
                                File.AppendAllText("imapArchive.log", ex.StackTrace & vbCrLf)
                            End Try
                        Next
I get error in line Dim soubor As String ....

Would you be so kind to write me, what is wrong?

Thank you
Miroslav
Mar 4, 2014 at 12:20 PM
Hi Miroslav,

the exception occurs because the Message.InternalDate property doesn't have a value. This is because it is not being fetched at all, as you have made a mistake setting the download mode.

It should be this way:
Enums.MessageFetchMode.None Or Enums.MessageFetchMode.InternalDate
Bit flags are used to identify the fetch mode. so you cannot use the + operator to combine them.

If you want to know more about it and how you can combine the different values, there is a good article on CodeProject: Understand how bitwise operators work (C# and VB.NET examples)

Greets,

Pavel
Mar 4, 2014 at 3:09 PM
Hi Pavel,
thanks for explanation.
Miroslav