Hi,
I'm trying to read lines from a file and split them into two words contained in a two dimensional array. The file looks something like this:
dog cat
red blue
orange green
night day
year month
circle square
-1
Here is my code:
Public Class Form1
Dim i As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim value() As String
Dim FILE_NAME As String = "C:\test.txt"
Dim TextLine As String
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim pos(,) As String
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
value = TextLine.Split(" ")
'When I run it, it highlights the following line and says Object reference not set to an instance of an object
pos(i, 0) = value(0)
pos(i, 1) = value(1)
i = i + 1
Loop
TextBox1.Text = pos(0, 0)
TextBox2.Text = pos(0, 1)
Else
MsgBox("File Does Not Exist")
End If
End Sub
End Class
Does anyone know why I'm getting this error?
I'm trying to read lines from a file and split them into two words contained in a two dimensional array. The file looks something like this:
dog cat
red blue
orange green
night day
year month
circle square
-1
Here is my code:
Public Class Form1
Dim i As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim value() As String
Dim FILE_NAME As String = "C:\test.txt"
Dim TextLine As String
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim pos(,) As String
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
value = TextLine.Split(" ")
'When I run it, it highlights the following line and says Object reference not set to an instance of an object
pos(i, 0) = value(0)
pos(i, 1) = value(1)
i = i + 1
Loop
TextBox1.Text = pos(0, 0)
TextBox2.Text = pos(0, 1)
Else
MsgBox("File Does Not Exist")
End If
End Sub
End Class
Does anyone know why I'm getting this error?