Sunday 15 March 2015

c# - An unhandled exception of type 'System.NullReferenceException' occurring when clicking TreeNode -


itemprop = "text">

I get this error message whenever a treenode is clicked:

< P> Exception type 'System.NullReferenceException' Image Resize.exe Additional information came in:. Object references are not set for an instance of an object

My code is as follows: Public Form 1 () {InitializeComponent (); This.treeView1.NodeMouseClick + = New TreeNodeMouseClickEventHandler (this.treeView1_NodeMouseClick); PopulateTreeView (); } Private Zero populateTreeView () {TreeNode rootNode; DirectoryInfo info = new DirectoryInfo (@ "c: \\"); If (info.Exists) {root Node = new tree node (info.Name); RootNode.Tag = info; GetDirectories (info.GetDirectories (), RoutodeD); TreeView1.Nodes.Add (rootNode); }} Private Zero GetDirectories (DirectoryInfo [] sub-dir, TreeNodeNodeToDo) {Trinode Anode; Directoryinfo [] sub subdivision; Forex currency (DirectoryInfo subdir in sub-directory) {aNode = new treeode (subdirectory name, 0, 0); ANode.Tag = subDir; ANode.ImageKey = "folder"; Try {/ subSubDirs = subDir.GetDirectories (); If (Sub Subbers. Long! = 0) {Gate Directory 2 (Subsbudyear, Anode); } * /} (System.UnauthorizedAccessException) {subSubDirs = New Directory Info [0]; } NodeToAddTo.Nodes.Add (aNode); }} Zero TreeView1_NodeMouseClick (Object Sender, TreeNodeMouseClickEventArgs E) {// (treeView1.SelectedNode.Nodes.Count> 0) {MessageBox.Show ("Child node exists"); } Other {message box. Show ("hair node does not exist"); } Try {New Annotated TreeNode = e.Node; // Tree View 1 Selected node. Nodes. Add ("test"); ListView1.Items.Clear (); DirectoryInfo nodeDirInfo = (DirectoryInfo) newly selected Tag; ListViewItem.ListViewSubItem [] Sub-Items; List view object = null; Foreach (directoryInfo dir in nodeDirInfo.GetDirectories ()) {item = new ListViewItem (dir.Name, 0); SubItems = new ListViewItem.ListViewSubItem [] {New ListViewItem.ListViewSubItem (item, "Directory"), New ListViewItem.ListViewSubItem (item, dir.LastAccessTime.ToShortDateString ())}; Item.SubItems.AddRange (subItems); ListView1.Items.Add (item); } Foreach (file infos file in nodedairfoin (file)) {items = new list view ITM (file name, 1); SubItems = new ListViewItem.ListViewSubItem [] {New ListViewItem.ListViewSubItem (item, "file"), New ListViewItem.ListViewSubItem (item, file.LastAccessTime.ToShortDateString ())}; Item.SubItems.AddRange (subItems); ListView1.Items.Add (item); } ListView 1. Auto resize column (column header auto resize style. Headers); } Hold (Exception pre) {If (formerly System.NullReferenceException is? Ex is System.UnuthorizedAccessException) {}}}

Exception handler catches it, but an exception because it was thrown Because the user needs to click twice to make click handler effective.

Why did I get this error, and how do I avoid it?

after "div class =" itemprop = "text">

  // if (treeView1.SelectedNode.Nodes.Count> gt; 0)   

This is the reason that is actually wrong. A node is not selected until the after that nodemogues event occurs in the fire. So this code is likely to be bombed with NRE, if no selection has been done yet and the first click needs to be clicked twice before the selected node.

You should use the e.Node property instead, it lets you click the node that was clicked. The intention of the code is a bit unclear, strong obstacles that you should actually use the AfterSelect event instead, so it also works when the user uses the keyboard to select the nodes.

No comments:

Post a Comment