Object Reference not set to an instance of an object.

Smokeywade

Member
Joined
Sep 11, 2007
Messages
22
Programming Experience
3-5
I have a treelistview that is populated with parent child. What I'm trying to do is when the user clicks on the parent place the subitems text into a textbox
VB.NET:
Expand Collapse Copy
TextBox1.Text = treeListView1.SelectedNodes(0).SubItems(4).Text & ""
Which works fine when I click on a parent, But when I click on the first record child I get error Object Reference not set to an instance of an object.

when I click on the second record child I get specified argument was out of range the valid values parmeter name: Index was out of range. Must be non-negative and less then the size of the collection.

Here is the simple code that dont work
Private Sub treeListView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles treeListView1.Click

VB.NET:
Expand Collapse Copy
Try
If treeListView1.SelectedNodes(0).SubItems(4).Text Is DBNull.Value Then
TextBox1.Text = ""
Else
TextBox1.Text = treeListView1.SelectedNodes(0).SubItems(4).Text & ""

End If
Catch ex As Exception
MsgBox(ex.Message)

End Try

End Sub

Any Ideas on how I will tell it - if child rec do nothing.

Thanks

Thank You
 
You just need to figure out what is nothing in that code. Set a breakpoint at that line of code and then hover the mousepointer over the different objects in that line. One of them will equal nothing, there's the reason for the error. Then you just need to figure out why it is nothing.
 
Yup it was looking for something that was not there
so I added 3 empty columns in the child.
I was telling it to get something that was not there in the child
VB.NET:
Expand Collapse Copy
TextBox1.Text = treeListView1.SelectedNodes(0).SubItems(4).Text & ""
This will only work in the parent because the parent has the four subitems not the child -
so after adding the 3 empty columns I no longer get the error Object Reference not set to an instance of an object. when clicking the first child

but I still get error specified argument was out of range the valid values parmeter name: Index was out of range. Must be non-negative and less then the size of the collection.
when clicking on the second record's child - I'm definetly missing something...

Thanks
 
Back
Top