I've been struggling with this for a while and finally gave in and am seeking some help.

The below runs on a `After Select` event in a treeview. The code runs absolutely fine but since I've made a slight adjustment to the process I'm getting an `Object not set to an instance of an object` error the first time the node is clicked. Once the error has been cleared the code runs as normal.

The error gets triggered on this line

Code:
Dim NodeFile As New IO.DirectoryInfo(Path.Combine(tempMail, tvProgress.SelectedNode.FullPath))
Like I say, once I've cleared the error the code runs absolutely fine.

Code:
      Try
            Dim da As New OleDb.OleDbDataAdapter("", "")
            Dim dt As New DataTable
            Dim aClients As String = My.Settings.ClientDB
            Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & aClients & ""
            Dim n As Integer
            Dim q As Integer
            Dim tempMail As String = My.Settings.Temporary
            Dim enUK As New CultureInfo("en-GB")

            lstFiles.Enabled = True
            lstFiles.Items.Clear()

            If (e.Node.Level) > 0 Then

                If e.Node.Parent.Text = "Archive" Then Call INIDate()

                If ndes.Contains(tvProgress.SelectedNode.Text) Then
                    For n = 0 To UBound(AllDetails)

                        Dim Datey As String = Nothing
                        If e.Node.Parent.Text <> "Archive" Then
                            Datey = Date.ParseExact(Convert.ToDateTime(e.Node.Parent.Text), "dd/MM/yy", enUK)
                        Else

                            For q = 0 To UBound(INIdet)
                                If e.Node.Parent.Text = "Archive" Then Datey = INIdet(q).iDate


                                Dim code As String = AllDetails(n).uCode
                                Dim op As String = AllDetails(n).uOps
                                Dim name As String = AllDetails(n).uName
                                Dim dte As String = Date.Parse(Datey).ToString("dd-MM-yyyy")

                                If e.Node.Text = name & " - " & code & " - " & op Then

                                    Dim txt2check As String = My.Settings.FileStream & code & "-" & op & "-" & dte & ".txt"
                                    If Not File.Exists(txt2check) Then
                                        Dim tw As StreamWriter = File.CreateText(My.Settings.FileStream & code & "-" & op & "-" & dte & ".txt") ' Create a file to write to. 
                                        tw.WriteLine("unitname=" & AllDetails(n).uName)
                                        tw.WriteLine("unitcode=" & AllDetails(n).uCode)
                                        tw.WriteLine("opername=" & AllDetails(n).uOps)
                                        tw.WriteLine("plandate=" & Datey)
                                        tw.Flush()
                                        tw.Close()
                                        Call TxtRfrsh()
                                        Call RfshArray()
                                    End If
                                End If
                            Next
                        End If


                        If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps & " - " & AllDetails(n).uPlan = e.Node.Text & " - " & Datey Then
                            Dim fldr As String = My.Settings.FileStream & AllDetails(n).uFile
                            With fldr
                                If My.Computer.FileSystem.DirectoryExists(fldr) Then

                                    Directory.Delete(fldr, True)
                                End If
                            End With


                            Application.DoEvents()

                            Dim NodeFile As New IO.DirectoryInfo(Path.Combine(tempMail, tvProgress.SelectedNode.FullPath))

                            Dim reports = NodeFile.EnumerateFiles().Select(Function(f) Path.GetFileName(f.Name)).ToList()

                            Dim newreport As String = String.Join("' AND Documents.DocName <> '", reports.ToArray())

                            If tvProgress.Nodes.Count = 0 Then Exit Sub

                            Dim eSearch As String = AllDetails(n).uCode
                            Dim fSearch As String = AllDetails(n).uOps
                            Dim gsearch As String = "'" & newreport & "'"

                            da.SelectCommand.Connection.ConnectionString = conn
                            da.SelectCommand.CommandText = "SELECT Documents.DocName FROM Documents WHERE (Documents.UnitCode = ?) AND (Documents.OpName = ?) AND (Documents.DocName <> " & gsearch & ") AND Documents.Required = True ORDER BY DocName"
                            da.SelectCommand.Parameters.AddWithValue("@p1", eSearch)
                            da.SelectCommand.Parameters.AddWithValue("@p2", fSearch)
                            da.Fill(dt)

                            dt.Rows.Add("Add Additional Requirement")
                            lstRequired.DataSource = dt
                            lstRequired.DisplayMember = "DocName"
                            lstRequired.Refresh()

                            Dim tFiles As String = My.Settings.LTFile
                            Dim dl As DataTable = CType(lstRequired.DataSource, DataTable)
                            Using sR = New IO.StreamReader(tFiles & UCase("ProgExcluded.txt"))
                                While (sR.Peek() > -1)
                                    Dim rows() = dl.Select("DocName = '" + sR.ReadLine + "'")
                                    For Each row In rows
                                        row.Delete()
                                    Next
                                    dl.AcceptChanges()
                                End While
                            End Using
                        End If
                    Next


                End If
            End If
            Exit Sub

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
Cross-posted here http://stackoverflow.com/questions/2...-treenode-only