Object not set to an instance

G

Guest

The following line is creating the above error message.

tree_nodes((level)-1).Nodes.Add(tree_nodes(level))

It's because of the following line where I'm trying to calculate the length of
blank spaces and substracting from total length.

level = text_line.Length - text_line.TrimStart.Length

Here's the code. The first part of the "IF" is fine.

Dim stream_reader As New System.IO.StreamReader(file_name)
Dim file_contents As String = stream_reader.ReadToEnd()
stream_reader.Close()

' Remove line feeds.
file_contents = file_contents.Replace(vbLf, "")

' Break the file into lines.
Const charCR As Char = CChar(vbCr)
'Const charTab As Char = CChar(vbTab)
Dim lines() As String = file_contents.Split(charCR)

' Process the lines.
Dim text_line As String
Dim level As Integer
Dim tree_nodes() As Microsoft.Web.UI.WebControls.TreeNode
Dim num_nodes As Integer = 0
ReDim tree_nodes(num_nodes)

trv.Nodes.Clear()
For i As Integer = 0 To lines.GetUpperBound(0)
text_line = lines(i)
If text_line.Trim().Length > 0 Then
' See how many tabs are at the start of the line.

level = text_line.Length - text_line.TrimStart.Length

' Make room for the new node.
If level > num_nodes Then
num_nodes = level
ReDim Preserve tree_nodes(num_nodes)
End If

' Add the new node.
If level = 0 Then
'tree_nodes(level) = trv.Nodes.Add(text_line.Trim())
tree_nodes(level) = New TreeNode
tree_nodes(level).Text = CStr(text_line.Trim())

trv.Nodes.Add(tree_nodes(level))

Else
' tree_nodes(level) = tree_nodes(level -
1).Nodes.Add(text_line.Trim())
tree_nodes(level) = New TreeNode
tree_nodes(level).Text = CStr(text_line.Trim())
tree_nodes((level)-1).Nodes.Add(tree_nodes(level))
End If

I'm reading from a file that has indentation based on spaces. If it was a
true tab-delimited file I would be okay. So, it's the blank spaces that
would determine order of the nodes. Thanks in advance.
 
K

Kevin Spencer

In the statement:

tree_nodes((level)-1).Nodes.Add(tree_nodes(level))

There are 4 object references. One of them is not an instance of an object.
The 4 references are:

tree_nodes
tree_nodes(level-1)
tree_nodes(level-1).Nodes
tree_nodes(level)

Assuming that tree_nodes is an instance of an object, and that if there is
an instance of a tree_node that it will have an instance of Nodes, you
actually have 2 object references to check:

tree_nodes(level-1)
tree_nodes(level)

That should narrow it down for you.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
G

Guest

Thanks for the reply. I'm sure it's the (level)-1 that is causing it. The
code was originally set to work on tab delimited and had the following
relationship.

Const charTab As Char = CChar(vbTab)
level = text_line.Length - text_line.TrimStart(vbTab).Length

Since I have spaces instead of tabs in the file I took out the "vbtab", and
that's what is causing error. Referencing the spaces instead of tabs is
where I'm hung up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top