I'm making a tool that reads a file into a string array and then edits the text of each array index before displaying all the lines of the string array in a RichTextbox control.
Here is my code so far:
Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
Dim ofd As New OpenFileDialog
ofd.FileName = ""
ofd.Title = "Open File"
If ofd.ShowDialog = DialogResult.OK Then
'read file
Dim reader As New StreamReader(ofd.FileName, Encoding.Default)
Dim line As String = Nothing : Dim counter As Integer = 0
Do
line = reader.ReadLine
readLines(counter) = line
counter += 1
Loop Until line Is Nothing
reader.Close() : counter = 0
'removing text from left
While counter < readLines.Length
readLines(counter) = readLines(counter).Substring(0, readLines(counter).Length - txtLimit.Text)
counter += 1
End While
counter = 0
'show result in RichTextBox
While counter < readLines.Length
rtBox.Text = rtBox.Text + readLines(counter) + vbNewLine
counter += 1
End While
ElseIf ofd.ShowDialog = DialogResult.Cancel Then
MsgBox("Operation Cancelled!")
Else
MsgBox("Unable to open file!")
End If
End Sub
Right below readLines(counter) = line
, where it says counter += 1
, I get this error:
System.NullReferenceException: Object reference not set to instance of Object
What am I doing wrong?