VB.NET System.NullReferenceException:Array为Null

时间:2014-06-02 08:52:24

标签: vb.net

这是我的完整代码:

Public Class ToDoList

    Dim ToDoListItems() As String
    Dim ToDoListContents() As String
    Dim ToDoListDates() As String
    Dim ToDoListInfo() As String

    Private Function FileExists(ByVal File As String)
        If My.Computer.FileSystem.FileExists(File) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function ContentOf(ByVal File As String)
        If FileExists(File) Then
            Return My.Computer.FileSystem.ReadAllText(File)
        End If
        Return ""
    End Function

    Private Sub WriteTo(ByVal File As String, ByVal Content As String, Optional ByVal NewLineBefore As Boolean = False)
        If FileExists(File) Then
            If NewLineBefore = False Then
                My.Computer.FileSystem.WriteAllText(File, Content, True)
            Else
                My.Computer.FileSystem.WriteAllText(File, Convert.ToString(Environment.NewLine + Content), True)
            End If
        End If
    End Sub

    Private Sub AddNewEvent(sender As Object, e As EventArgs) Handles AddNewEventButton.Click
        NewEvent.Show()
    End Sub

    Private Sub RefreshApplication() Handles MyBase.Load, RefreshBtn.Click
        RefreshToDoList()

        For i As Integer = 0 To ToDoListItems.Length - 1
            ToDoListInfo(i) = "Date : " + ToDoListDates(i) + Environment.NewLine + Environment.NewLine + "Content : " + ToDoListContents(i)
        Next
    End Sub

    Private Sub RefreshToDoList()
        ToDoListBox.Items.Clear()

        If FileExists(NewEvent.ToDoItems_Path) And FileExists(NewEvent.ToDoDates_Path) And FileExists(NewEvent.ToDoContents_Path) Then
            If Not ContentOf(NewEvent.ToDoItems_Path) = "" Then
                ToDoListItems = My.Computer.FileSystem.ReadAllText(NewEvent.ToDoItems_Path).Split(New String() {Environment.NewLine}, StringSplitOptions.None)
                ToDoListDates = My.Computer.FileSystem.ReadAllText(NewEvent.ToDoDates_Path).Split(New String() {Environment.NewLine}, StringSplitOptions.None)
                ToDoListContents = My.Computer.FileSystem.ReadAllText(NewEvent.ToDoContents_Path).Split(New String() {Environment.NewLine}, StringSplitOptions.None)
            End If

            If ToDoListItems.Length = ToDoListDates.Length And ToDoListItems.Length = ToDoListContents.Length Then
                For count As Integer = 0 To ToDoListItems.Length - 1
                    ToDoListBox.Items.Add(ToDoListItems(count))
                Next
            End If
        End If
    End Sub

    Private Sub ToDoListBox_SelectNewItem(sender As Object, e As EventArgs) Handles ToDoListBox.SelectedIndexChanged
        If Not ToDoListBox.SelectedIndex = -1 Then
            EventContent.Text = ToDoListInfo(ToDoListBox.SelectedIndex)
        End If
    End Sub
End Class

当事件ToDoListBox.SelectedIndexChanged发生时,有一个System.NullReferenceException。我发现ToDoListInfo为空。

RefreshApplication()中的for循环有什么问题?我想这里发生了一些事情,但我不知道如何编辑它。请帮我!任何帮助将不胜感激。

感谢。

0 个答案:

没有答案