Treeview question #4

Hi

Back again. This question is typed boolean :)

I had problems when I click on the last node of a branch in treeview. This would invariably cause an 'Object reference not set to an instance of an object.' error.

To solve this, I wrapped the offending code in an 'Try...Catch exception' = No more of those errors.

Question: Is doing this a 'cop out'

If answer = True then why

I had tried a number of other 'traps' to avoid the offending code being executed, but all those attempts were trying to use aspects of the same variable(s) etc which also caused exceptions.




Answer this question

Treeview question #4

  • bobby s

    hi,

    the last node in the treeview could cause a trouble , it gave me a headache in the past but unfortunatly i don't remember how i did solve it, but i googled for it and i found some solutions

    hope this helps



  • trustfundbaby

    Leshay,

    You should always be able to interogate it.

    I've been doing a lot of this lately, and the only errors I've had had really been stale data.

    In other words, in a complex datastructure in Node.tag, that data structure has a record number. I'd better make sure I don't to anything to the able that changes the record number without refreshing that structure.

    But if the algorithm is

    Check for data

    If I have it instantiate a node, put data in it and add it

    It's difficult to go wrong.

    Btw, congrats on getting rid of your giant font problem. If you have WORD,

    you can cut and past from the editor in Word and then cut and past again into this editor and that will preserved identitation and that will make your code easier to read. It's really hard when it's up against the left margin like that. :)



  • dotAge

    Hi

    To further explain: I have a Treeview populated with folders/files. Any folders are nodes, mostly with children. The tree nodes are populated on form load with folders only. Then during run time, if I click on a node, the node click event handler finds and adds any files to the node.

    It is these - the files - which if clicked on that cause the error. The error occurs as below:

    Sub DoMouseClick(ByVal n As TreeNode) 'called from treeView1_NodeMouseClick

    Dim newSelected As TreeNode = n

    Dim nodeDirInfo As DirectoryInfo = CType(newSelected.Tag, DirectoryInfo)

    Dim aNode As TreeNode

    Dim file As FileInfo

    Dim i As Integer, flag As Boolean = False

    For Each file In nodeDirInfo.GetFiles()  --------- error here

    and happens when file = Nothing. Now I understand that, but what I don't understand is how to stop it from happening when 'file' is not assigned until the error line itself I  can't test 'file' as it will always be Nothing until the offending line, and I can't figure out how to test 'nodeDirInfo.GetFiles()'.

    Ken: Your posted suggestion doesn't allow me to open the node at all if it is the last folder node (it hasn't yet been populated until it first goes through this routine).

    All of this is to try to make the program usable. If I populated the whole tree at the start, it takes forever to scan through and populate the tree.



  • iCore

    I'm a little confused because a last node problem is one that I haven't had.

    The sequence i've always used is:

    Is there data

    Yes - instantiate a node & add it.

    But not without data in the first place.



  • Krysys

    I hate to one question you to death on this...

    But are you populating the entire treeview at load time or just the 0th level



  • sap22

    Hi

    Yes, that is what I thought. The very reason for the post. It did seem to be the 'easy way out'. Now I have to bug you all with trying to sort out what the bug is :)



  • Earl Seugling

    ReneeC

    Yes, that is done when the form load calls a routine to populate the tree with the folders only.

    I was trying, as per the 'sticky' not to post too much code, but I suppose I need to here, to answer your question.

    Private Sub PopulateTreeView() ---------- called on form load

    Dim rootNode As TreeNode

    TreeView1.ImageList.Images.Add("folder", My.Resources.folder)

    TreeView1.ImageList.Images.Add("doc", My.Resources.doc)

    Dim info As New DirectoryInfo(StartUpPath)

    If info.Exists Then

    rootNode = New TreeNode(info.Name)

    rootNode.Tag = info

    GetDirectories(info.GetDirectories(), rootNode)

    TreeView1.CheckBoxes = True

    TreeView1.Nodes.Add(rootNode)

    End If

    End Sub

    Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, _

    ByVal nodeToAddTo As TreeNode)

    Dim aNode As TreeNode

    Dim subSubDirs() As DirectoryInfo

    Dim subDir As DirectoryInfo

    For Each subDir In subDirs

    If Not (GetAttr(subDir.FullName) And FileAttribute.System) = FileAttribute.System Then

    aNode = New TreeNode(subDir.Name)

    aNode.Tag = subDir

    aNode.ImageKey = "folder"

    subSubDirs = subDir.GetDirectories()

    If subSubDirs.Length > 0 Then

    GetDirectories(subSubDirs, aNode)

    End If

    nodeToAddTo.Nodes.Add(aNode)

    End If

    Next subDir

    End Sub



  • bambag

    Having your code in a try catch block is always a good idea. I would always try and prevent an error from happening because exceptions are slow. I can not see your code but you are saying the errors occur on the last branch in a tree view. Maybe only running the code treenode.nodes.count>0 will prevent the error.


  • John Nekrews

    shakalama

    I actaully did have a solution as such using a Try ... Catch exception block, but it seems that just hides bad code. I am trying to find out what the bad code is rather than use the 'quick solution'.

    Run into a brick wall when trying to deal with the offending code though :)



  • scubabeme

    It's not good practice.

    There is an error in your code. Fix the error. Don't hide the bug.



  • Gregory A. Beamer - MVP

    Maybe my methods are suspect then.

    The program is admittedly a mish-mash taken mostly from the help system, and from here on the forum.

    I still wonder, from the node passed into the procedure, why I can't interrogate it to see if it represents the last in the line of nodes, without causing an error. Or, alternatively, if it has children, without causing an error.

    Maybe I need to do a rethink of the overall program structure I am using.



  • RyuoJH

    No, only the folders are created at the start. Then, when a folder node is clicked, it populates the files contained therein.

  • Drummos

    Exactly....

    I don't think you need a Try-Catch block there. I think you need to test for the condition before attemtpting to use an object that really isn't there.



  • Claudionir Silva

    Let me see if I understand what you're doing first.....

    You are storing a dirinfo in a treenode.tag



  • Treeview question #4