Error: Object reference not set... (VB)

VinceC

Newcomer
Joined
May 30, 2003
Messages
15
Location
Ontario, Canada
Error: Object reference not set...

I am using VB.Net (Standard Ed.), my program opens a dialog window for selection of a text file (containing numbers) and reads it in as a "stream". It then tries to convert the stream from text to double (unsuccessfully). The error that I get is on the line of code below where it happens. Here is my code, can someone help?

Visual Basic:
'Code from my Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


            Dim OpenFileDialog1 As New OpenFileDialog()
            OpenFileDialog1.InitialDirectory = "c:\"
            OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            OpenFileDialog1.FilterIndex = 2

            Dim sreader As IO.StreamReader

            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                sreader = IO.File.OpenText(OpenFileDialog1.FileName)
                CreateArray(sreader)
                sreader.Close()
            End If

        End Sub
    End Class

'Code from my Module

    Sub CreateArray(ByVal streamM As IO.StreamReader)

        Dim x As Integer
        Dim q()
        Dim z() As Double
        Dim Efile As String
        Efile = streamM.ReadToEnd
        q = Split(Efile, vbCrLf)

        For x = 1 To q.Length - 1
            z(x) = CDbl(q(x))   'Error I receive: "Object reference not set to an instance of an object."
        Next x
    End Sub

Thanks for any help.
 
Last edited by a moderator:
Not got VB.Net on the machine in front of me so this hasn't been tested.

On the line you are getting the error z(x) probably doesn't point ot a valid array element.

a few lines previous you are declaring
Visual Basic:
Dim z() As Double

but at no point are you sizing the array.

you may want to try the following and see if it helps
Visual Basic:
 Sub CreateArray(ByVal streamM As IO.StreamReader)

Dim x As Integer
Dim q()
Dim z() As Double
Dim Efile As String
Efile = streamM.ReadToEnd
q = Split(Efile, vbCrLf)

redim z(q.Length - 1)          'Add this line here
For x = 1 To q.Length - 1
z(x) = CDbl(q(x)) 'Error I receive: "Object reference not set to an instance of an object."
Next x
End Sub
 
Wow. Thanks for the quick response. It worked. I should have caught that, makes perfect sense. Just starting to use .NET so feeling like a fish out of water.
 
Also, I feel I must point out the icky-ness of using the 'Split' command,
as it is left over from VB6 for compatability reasons. Instead, the
string commands are actually methods of the String class.

in VB6:
Visual Basic:
Dim a As String
Dim b As String

b = "Hello There!"
a = Split$("Hello There!", " ")

in VB.NET:
Visual Basic:
Dim a, b As String

b = "Hello There!"
a = b.Split(" ")
There is more info about this stuff in the MSDN included with .NET,
or online at msdn.microsoft.com.



I recommend you play around with C#, because it forces you to do
things the proper .NET way, taking away those old VB6 commands.
 
Thanks for the advice. I'm going to try the .split method you mentioned. I'm also going to start learning a little about C# so that I can gradually make the transition from VB.NET. After reading a few posts here and there it seems C# is the better way to go?
 
Back
Top