Reading Lines and Splitting them into a 2 dimensional array

jasonddd3

New member
Joined
Jun 23, 2011
Messages
2
Programming Experience
Beginner
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?
 
Jason,

You declared your array, but then forgot to actually create or initialize it. Try something like:

Const MaxWordSets As Integer = 100
Const WordsPerSet As Integer = 2
Dim pos(,) As String
pos = New String(MaxWordSets - 1, WordsPerSet - 1) {}


Mark
 
Back
Top