Visual Studio 2017 Value不能为null。参数名称:路径

时间:2018-03-24 21:15:00

标签: vb.net nullreferenceexception

我正在构建一个显示总成绩和班级平均值的应用,结果存储在 .txt 文件中。当我点击“显示平均值”按钮时,我会收到此错误代码:

  

“值不能为空。参数名称:路径”。

我做错了什么?我已经添加了问题的图片以及我的所有代码。

The snapshot of the error

Imports System.IO' using classes from this namepace

Public Class EnhancedClassAverage
    Dim grade As Integer
    Dim fileWriter As StreamWriter ' writes data to text file
    Dim fileReader As StreamReader ' displays data
    Dim fileName As String ' name of file containning grade data
    Private Sub clearGradesButton_Click(sender As Object, e As EventArgs) Handles clearGradesButton.Click
        gradesListBox.Items.Clear() ' removes all items from gradesListBox
        classAverageLabel.Text = String.Empty ' clears classAverageLabel
    End Sub

    Private Sub gradesListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gradesListBox.SelectedIndexChanged

    End Sub
    ' place a grade in te gradesListBox
    Private Sub submitGradeButton_Click(sender As Object, e As EventArgs) Handles submitGradeButton.Click
        grade = Convert.ToInt32(gradeTextBox.Text) ' get grade from text box
        displayGrade(grade) ' calls function
        gradeTextBox.Text = " "
    End Sub

    Sub displayGrade(grade As Integer)
        gradesListBox.Items.Add(grade) ' adds grade to listbox
        fileWriter.WriteLine(grade) ' write grade to file
    End Sub
    Private Sub calculateAverageButton_Click(sender As Object, e As EventArgs) Handles calculateAverageButton.Click
        CloseFile()

        Dim total As Integer = 0 ' sum of grades entered by user
        Dim gradeCounter As Integer = 0 ' counter for grades
        Dim average As Double ' average of grades
        Dim fgrade As Integer ' grade from file
        Dim fileReader As StreamReader = Nothing  ' read from file
        fileReader = New StreamReader(fileName) ' open file for reading

         'processing phase
        Do While Not fileReader.EndOfStream ' read until the end of the file
            fgrade = Convert.ToInt32(fileReader.ReadLine) ' get next grade
            total += fgrade ' add grade to total
            gradeCounter += 1 ' add 1 to gradeCounter
        Loop

        average = total / gradeCounter ' calculate average

        'display total and average 
        classAverageLabel.Text = "Total of the " & gradeCounter &
                " grade(s) is " & total & vbCrLf & "Class average is " &
                String.Format("{0:F}", average)

    End Sub

    Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
        CloseFile() ' ensure that any prior file is closed
        Dim result As DialogResult ' stores reault of Save dialog
        Dim fileName As String ' name of file to save data
        'diplay diaolg so user can choose the name of the file to save
        Using fileChooser As New SaveFileDialog()
            result = fileChooser.ShowDialog()
            fileName = fileChooser.FileName ' get specified file name

        End Using

        ' if user did not click Cancel
        If result <> Windows.Forms.DialogResult.Cancel Then
            Try
                fileWriter = New StreamWriter(fileName, True) ' open or create file for writing

                'enable controls
                CloseToolStripMenuItem.Enabled = True
                submitGradeButton.Enabled = True
                clearGradesButton.Enabled = True
                gradesListBox.Enabled = True
                gradeTextBox.Enabled = True
                calculateAverageButton.Enabled = True

            Catch ex As IOException
                MessageBox.Show(MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        CloseFile() ' closes file before terminating app
        Application.Exit() ' terminates the app
    End Sub

    ' close the file
    Sub CloseFile()
        If fileWriter IsNot Nothing Then
            Try
                fileWriter.Close() ' close streamWriter
            Catch ex As IOException
                MessageBox.Show("Error closing file", "Error",
                 MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Private Sub CloseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseToolStripMenuItem.Click

        CloseFile() ' close currently open file
        CloseToolStripMenuItem.Enabled = False
        submitGradeButton.Enabled = False
        clearGradesButton.Enabled = False
        gradesListBox.Enabled = False
        gradeTextBox.Enabled = False
        calculateAverageButton.Enabled = False
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

你看到你的位置

Dim fileName As String ' name of file containning grade data

靠近顶部?

然后你有

Dim fileName As String ' name of file to save data
Sub NewToolStripMenuItem_Click中的

虽然变量名称相同,但它们不引用同一个变量,因为第二个声明的范围是它所在的Sub。所以fileName的值Sub calculateAverageButton_Click中没有给出值。

您可以删除Sub。中的Dim