VB.NET 无法读取字符串数组中的文件
VB.NET Unable to read file in string array
我正在制作一个工具,将文件读入字符串数组,然后编辑每个数组索引的文本,然后在 RichTextbox 控件中显示字符串数组的所有行。
到目前为止,这是我的代码:
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
在 readLines(counter) = line
正下方,上面写着 counter += 1
,我收到此错误:
System.NullReferenceException: Object reference not set to instance of Object
我做错了什么?
我怀疑原来的问题是 readLines
数组没有正确初始化。代码试图写入数组末尾,而调试器出于某种原因在错误的行上显示了问题。
Dim
一个数组是不够的。那只是设置了一个参考变量,但那个变量仍然是null
/Nothing
。您还必须创建新的(提示)数组对象。
也就是说,这个功能可以减少很多。
Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
Dim ofd As New OpenFileDialog
ofd.Title = "Open File"
Dim result As DialogResult = ofd.ShowDialog()
If result = DialogResult.Cancel Then
MsgBox("Operation Cancelled!")
Exit Sub
ElseIf result <> DialogResult.Ok
MsgBox("Unable to open file!")
Exit Sub
End If
Dim lines = File.ReadLines(ofd.FileName).Select(Function(line) line.Substring(0, line.Length - CInt(txtLimit.Text)) & vbNewLine)
For Each line As String In Lines
rtBox.Text &= line
Next
End Sub
我正在制作一个工具,将文件读入字符串数组,然后编辑每个数组索引的文本,然后在 RichTextbox 控件中显示字符串数组的所有行。
到目前为止,这是我的代码:
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
在 readLines(counter) = line
正下方,上面写着 counter += 1
,我收到此错误:
System.NullReferenceException: Object reference not set to instance of Object
我做错了什么?
我怀疑原来的问题是 readLines
数组没有正确初始化。代码试图写入数组末尾,而调试器出于某种原因在错误的行上显示了问题。
Dim
一个数组是不够的。那只是设置了一个参考变量,但那个变量仍然是null
/Nothing
。您还必须创建新的(提示)数组对象。
也就是说,这个功能可以减少很多。
Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
Dim ofd As New OpenFileDialog
ofd.Title = "Open File"
Dim result As DialogResult = ofd.ShowDialog()
If result = DialogResult.Cancel Then
MsgBox("Operation Cancelled!")
Exit Sub
ElseIf result <> DialogResult.Ok
MsgBox("Unable to open file!")
Exit Sub
End If
Dim lines = File.ReadLines(ofd.FileName).Select(Function(line) line.Substring(0, line.Length - CInt(txtLimit.Text)) & vbNewLine)
For Each line As String In Lines
rtBox.Text &= line
Next
End Sub