0

I'm building an app that displays the total grades and the class average, and the results are stored in a .txt file. When I click the "Show average" button I'm given this error code:

"Value cannot be null. Parameter name: path".

What am I doing wrong? I've add a picture of the problem as well as all of my code.

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
GSerg
  • 76,472
  • 17
  • 159
  • 346

1 Answers1

1

You see where you have

Dim fileName As String ' name of file containning grade data

near the top?

And then you have

Dim fileName As String ' name of file to save data

in Sub NewToolStripMenuItem_Click.

Although the variable names are the same, they do not refer to the same variable because the the scope of the second declaration is the Sub it is in. So the value of fileName in Sub calculateAverageButton_Click has not been given a value.

You could remove the Dim line in the Sub.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84