Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   Object reference error (http://www.programmingforums.org/showthread.php?t=14106)

Jabo Oct 4th, 2007 4:04 AM

Object reference error
 
I'm trying to make a parser for a text file, but I'm getting an object reference error. Can somebody tell me why?
:

Dim filereader As String
        Dim parser() As String
        Dim ArrSize, i, j, x As Integer
        Dim word(), newword() As Char

        filereader = My.Computer.FileSystem.ReadAllText("m:\VC++Express\Open Media Distribution List[1].txt")
        parser = Split(filereader)
        ArrSize = parser.Length
        For i = 0 To ArrSize - 1
            word = parser(i).ToCharArray
            x = word.Length
            For j = 0 To x - 1
                If Char.IsLetterOrDigit(word(j)) Then
                    newword(newword.Length) = word(j)
                End If
            Next
            parser(i) = newword.ToString
            lboTSMTape.Items.Add(parser(i))
        Next


The red line has error attached to it saying object reference not set to an instance of an object

melbolt Oct 4th, 2007 10:02 AM

let me see if I can explain this right.

here's the problem, you attempt to put something in the array newword but you never allocated any space for that array.

what i mean by this is, you never gave it bounds ex: newword(5) would allocate 5 indexes.

what you can do is this...
:

  1.           word = parser(i).ToCharArray
  2.           Dim newword(word.Length) As Char


the reason you didn't have to set up the word array like this is because you are not trying to utilize its indexes until you set it equal to an existing array(which does have allocated memory)
ex:
:

  1. word = parser(i).ToCharArray


Jabo Oct 4th, 2007 10:07 AM

Ahh, that makes a lot of sense, thanks melbolt. But now i'm gonna have to rethink my algorithm because the array will be different each time. Can you dynamically allocate a vector? I need to re-learn how to do a vector.

Jabo Oct 5th, 2007 3:37 AM

Here's what I wound up doing; it's not elegant, but it does the job.
:

Dim filereader As String
        Dim parser() As String
        Dim ArrSize, i, j, x, y As Integer
        Dim word(30), newword(30) As Char

        filereader = My.Computer.FileSystem.ReadAllText(cboFileList.Text)
        parser = Split(filereader)
        ArrSize = parser.Length

        For i = 0 To ArrSize - 1
            word = parser(i).ToCharArray
            x = word.Length
            y = 0
            For j = 0 To 29
                newword(j) = ""
            Next
            For j = 0 To x - 1
                If (Char.IsLetterOrDigit(word(j))) Then
                    newword(y) = word(j)
                    y = y + 1
                End If
            Next
            parser(i) = newword
            If parser(i).Substring(0, 2) = "CD" Or parser(i).Substring(0, 2) = "00" Then
                lboTSMTape.Items.Add(parser(i))
            End If
        Next



All times are GMT -5. The time now is 3:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC