How do I get rid of "Object reference not set to an instance of an object"?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • toyaxxx
    New Member
    • Sep 2008
    • 2

    How do I get rid of "Object reference not set to an instance of an object"?

    Hello guys,

    I have hit a dead end in trying to solve this problem. The thing is, I can't run the the program here because it has to run on a server which is in another place. All I receive here is a logfile with exceptions that happens when they test the program on the server.

    I'm using VS2005, VB.NET, XP Pro, CDO 1.21 dll
    Anyway, this function Logs on and traverses through "Public Folders" until it finds a folder I'm looking for.

    I hope someone could help me cause I need to resolve this fast.

    Here is the source code:

    Code:
    Public Sub Logon(ByVal profile As String, ByVal RootFolder As String)
    
            m_Session = New MAPI.Session
            m_Session.Logon(profile, , False, , , , )
    
            ' Go the fax store
            Dim Folders As String() = RootFolder.Split("/".ToCharArray())
    
            Dim i As Integer
            Dim objInfoStores As Object
            Dim objPublicFolderRoot As Object
            Dim objParent As Object
            Dim Parent As MAPI.Folder
    
            objInfoStores = m_Session.InfoStores
            m_InfoStores = DirectCast(objInfoStores, MAPI.InfoStores)
    
            For i = 1 To CInt(m_InfoStores.Count)
                Dim InfoStoreName As String
                objPublicFolderRoot = m_InfoStores.Item(i)
                InfoStoreName = m_InfoStores.Item(i).ToString
                If InfoStoreName = Folders(0) Then
                    m_PublicFolderRoot = DirectCast(objPublicFolderRoot, MAPI.InfoStore)
                    Exit For
                End If
            Next
    
            objParent = m_PublicFolderRoot.RootFolder
            Parent = DirectCast(objParent, MAPI.Folder)
    
            For i = 1 To Folders.Length - 1
                Parent = GetFolderByName(Folders(i), Parent)
            Next
            m_FaxRoot = Parent
    
        End Sub
    and this is the code for the GetFolderByName Function

    Code:
    Friend Function GetFolderByName(ByVal folderName As String, Optional ByVal parent As MAPI.Folder = Nothing) As MAPI.Folder
            Dim CdoSession As MAPI.Session = m_Session
            Dim CdoInfoStores As MAPI.InfoStores
            Dim CdoInfoStore As MAPI.InfoStore
            Dim CdoFolderRoot As MAPI.Folder
            Dim CdoFolders As MAPI.Folders
            Dim CdoFolder As MAPI.Folder
            Dim bFound As Boolean
    
            Dim i As Integer = 1
            Dim objInfoStores As Object
            Dim objInfoStore As Object
            Dim objFolderRoot As Object
            Dim objFolders As Object
            Dim objParentFolders As Object
            Dim objFolder As Object
            
            If parent Is Nothing Then
                objInfoStores = CdoSession.InfoStores
                CdoInfoStores = DirectCast(objInfoStores, MAPI.InfoStores)
                objInfoStore = CdoInfoStores.Item("Public Folders")
                CdoInfoStore = DirectCast(objInfoStore, MAPI.InfoStore)
    
                For i = 1 To CInt(CdoInfoStores.Count)
                    Dim InfoStoreName As String
                    objInfoStore = CdoInfoStores.Item(i)
                    InfoStoreName = CdoInfoStores.Item(i).ToString
                    If InfoStoreName = "Public Folders" Then
                        CdoInfoStore = DirectCast(objInfoStore, MAPI.InfoStore)
                        Exit For
                    End If
                Next
    
                objFolderRoot = CdoInfoStore.RootFolder
                CdoFolderRoot = DirectCast(objFolderRoot, MAPI.Folder)
                objFolders = CdoFolderRoot.Folders
    
                CdoFolders = DirectCast(objFolders, MAPI.Folders)
            Else
                ' Get the Folders collection from the parent folder.
                objParentFolders = parent.Folders
                CdoFolders = DirectCast(objParentFolders, MAPI.Folders)
            End If
    
            ' Loop through the folders in the collection until the
            ' desired folder is found.
            bFound = False
            objFolder = CdoFolders.GetFirst
            CdoFolder = DirectCast(objFolder, MAPI.Folder)
            Do While (Not bFound) And Not (CdoFolder Is Nothing)
                If CdoFolder.Name.ToString = folderName Then
                    bFound = True
                Else
                    objFolder = CdoFolders.GetNext
                    CdoFolder = DirectCast(objFolder, MAPI.Folder)
                End If
    
            Loop
    
            GetFolderByName = CdoFolder
    
            ' Release our local objects.
            CdoFolder = Nothing
            CdoFolders = Nothing
            CdoFolderRoot = Nothing
            CdoInfoStore = Nothing
            CdoInfoStores = Nothing
    End Function ' GetFolderByName
    Please help,
    JAN
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    Hi Jan,

    As I see the topicname, the first thing I think is: "He forgot to use 'New'". This goes for new objects instances. Try using

    Code:
    Dim .... as [B]New[/B] Object
    Now, as I don't know exactly what exceptions you get it's hard to tell where you should and shouldn't use them, but at least this is a start. You can try posting the list of exceptions...

    Comment

    • toyaxxx
      New Member
      • Sep 2008
      • 2

      #3
      Hi! Thanks for the reply

      This is the only exception i got from the testers

      Object reference not set to an instance of an object.
      at CDOMessaging.Ex changeConnector .Logon(String profile, String RootFolder)
      at InFaxAlert.InFa xAlert.OnWakeup 2(Object sender, ElapsedEventArg s e)

      I'll try placing "New" in my Object declarations
      Like this right?
      Code:
      Dim objInfoStores As New Object
      Thanks,
      JAN

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        Originally posted by toyaxxx
        Hi! Thanks for the reply

        This is the only exception i got from the testers

        Object reference not set to an instance of an object.
        at CDOMessaging.Ex changeConnector .Logon(String profile, String RootFolder)
        at InFaxAlert.InFa xAlert.OnWakeup 2(Object sender, ElapsedEventArg s e)

        I'll try placing "New" in my Object declarations
        Like this right?
        Code:
        Dim objInfoStores As New Object
        Thanks,
        JAN
        It's easier to test it instead of asking me if it looks right ;-) It does look right, but I'm not sure whether or not it will work...

        Comment

        Working...