Results 1 to 10 of 10

Thread: Object not set to an instance error on first select of treenode only

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    40

    Object not set to an instance error on first select of treenode only

    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

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Object not set to an instance error on first select of treenode only

    Put a breakpoint in that line and when it stops, look at the values of tempMail and tvProgress, one of those is empty.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    40

    Re: Object not set to an instance error on first select of treenode only

    Thanks, tried that and its the tvProgress.SelectedNode.FullPath which is empty. Don't quite understand as the path exists?

  4. #4
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Object not set to an instance error on first select of treenode only

    It should be the way you are assigning the object to tvProgress, if that object has not been set, it does not mater if the Path Exists
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Object not set to an instance error on first select of treenode only

    Your code needs some serious re writing, firstly getting rid of the doevents. You even checking My.Settings.Temporary has a value?
    My Github - 1d3nt

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Object not set to an instance error on first select of treenode only

    Wrapping the whole code in a Try block is not exactly a good idea also.
    My Github - 1d3nt

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    40

    Re: Object not set to an instance error on first select of treenode only

    I'm just learning with this. I've removed both suggestions from ident. I am aware that the code could be written far better but this is a first attempt so I'm just finding my feet and this works for me at the moment. I'm struggling to understand how I'm assigning the object to tvprogress incorrectly though.

    I'll keep on reading and trying

  8. #8
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Object not set to an instance error on first select of treenode only

    On second thought, it must be the selected object, not the tvprogress
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Object not set to an instance error on first select of treenode only

    Put a break point on Dim tempMail As String = My.Settings.Temporary whats the value?
    My Github - 1d3nt

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    40

    Re: Object not set to an instance error on first select of treenode only

    It gives the directory C:\working.

    As kaliman79912 suggested with his location of a Breakpoint its the tvprogress.selectednode.fullpath thats empty. Once the error is suppressed and the node reselected the code operates properly.

    This error only happens if the text file (text2check) has to be created from a date located in an .INI file and falls under the parent Node 'Archive'

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width