Object not set to an instance

Discussion in 'ASP .Net' started by =?Utf-8?B?SHV0dHk=?=, Feb 22, 2005.

  1. 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.

    --
    Hutty
     
    =?Utf-8?B?SHV0dHk=?=, Feb 22, 2005
    #1
    1. Advertising

  2. 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.

    "Hutty" <> wrote in message
    news:...
    > 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.
    >
    > --
    > Hutty
     
    Kevin Spencer, Feb 22, 2005
    #2
    1. Advertising

  3. 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.
    "Kevin Spencer" wrote:

    > 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.
    >
    > "Hutty" <> wrote in message
    > news:...
    > > 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.
    > >
    > > --
    > > Hutty

    >
    >
    >
     
    =?Utf-8?B?SHV0dHk=?=, Feb 23, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parthiv Joshi
    Replies:
    2
    Views:
    695
    Kalyan
    Jul 2, 2004
  2. Suresh Kojhani
    Replies:
    1
    Views:
    2,387
    Anushi
    Jul 29, 2004
  3. Chris Fink
    Replies:
    2
    Views:
    4,094
    David Waz...
    Jul 3, 2003
  4. yysiow
    Replies:
    1
    Views:
    436
    Kevin Spencer
    Jul 12, 2003
  5. Tim
    Replies:
    1
    Views:
    497
    Bill Priess
    Jul 17, 2003
Loading...

Share This Page