0

I am trying to make a program that takes a text from a file and writes it into a label in visual studios 10. I want to be able to click buttons on the exe to make it go from the previous line to the next line and vise versa. I am storing the text into an array and then making the label equal the text on the given part of the array. I am receiving the error "Object reference not set to an instance of an object." Can anyone help ? thanks. here is part of my code my code:

   Private Sub BrowseButton_Click(sender As System.Object, e As System.EventArgs) Handles BrowseButton.Click

            Dim UserInput As DialogResult = Browser.ShowDialog()

            If UserInput = Windows.Forms.DialogResult.Cancel Then
                Return
            End If

            FileOpen(1, Browser.FileName, OpenMode.Input)
            Do While Not EOF(1)
                Input(1, InternalTextFile(Index))
                Index += 1
            Loop
            FileClose(1)
            Output1Text.Text = InternalTextFile(Index)
            Output2Text.Text = InternalTextFile(Index + 1)

    End Sub

The error arises On the line Input(1, InternalTextFile(Index))

4

2 回答 2

0

看起来你可能想要这样的东西:

Private Sub BrowseButton_Click(sender As System.Object, e As System.EventArgs) Handles BrowseButton.Click
    If Browser.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

    Dim InternalTextFile() As String = File.ReadAllLines(Browser.FileName)
    Output1Text.Text = InternalTextFile(InternalTextFile.Length - 2)
    Output2Text.Text = InternalTextFile(InternalTextFile.Length - 1)
End Sub
于 2012-11-01T15:19:42.257 回答
0

没有看到更多代码很难说,但如果错误是 on Input(1, InternalTextFile(Index)),则很可能该变量InternalTextFile从未被赋值。可能是这样的?

Dim InternalTextFile As New List(of String)
Dim reader As StreamReader = New StreamReader(Browser.FileName)
Try
    Do 
        InternalTextFile.Add(reader.ReadLine)
    Loop Until reader.Peek = -1
Finally
    reader.Close()
End Try
于 2012-11-01T15:06:51.343 回答