I m in trouble with an Exception handling message

May 3 2012 9:04 AM
Hi everyone

I have coded the following class  then when i m trying to compile  i m receiving the following error message (in the FillList procedure  at the following line  : LstCooking.Items.Add(ck))


"ArgumentNullException  was Unhandled "


Please help

Thanks and regards


Armand



Imports System.IO
Imports System.IO.File

Public Class Form1

    Private ReadOnly mFilePath As String = "..\..\cooking.txt"
    Private allcookings() As Cooking

    Private Sub Form1_Load() Handles MyBase.Load

        GetFile()
        FillList()
    End Sub

    Private Sub FillList()

        LstCooking.Items.Clear()
        For Each ck As Cooking In allcookings
            LstCooking.Items.Add(ck)                        <--------------
        Next
    End Sub

    Public Function GetFile() As Boolean
        ' Read the data file, try to find the account ID.
        ' If found, read the account name and balance and
        ' return True. If not found, return false.
        ' An array is used to hold the workshops, since

        'not all students may
        ' be familiar with collections.

        Dim infile As StreamReader = Nothing
        LblResult.Text = String.Empty
        Try
            infile = OpenText(mFilePath)
            Dim numVals As Integer = CInt(infile.ReadLine)
            ReDim allcookings(numVals - 1)

            For i As Integer = 0 To numVals - 1
                Dim entireLine As String = infile.ReadLine()
                Dim fields() As String = entireLine.Split("\"c)
                Dim cook As New Cooking
                cook.mcategory = CType(fields(0), Cooking.CookingType)
                cook.CookingTime = CInt(fields(1))
                cook.mcost = CDbl(fields(2))
                cook.mtitle = fields(3)
                allcookings(i) = cook
            Next
            Return True
        Catch ex As Exception
            LblResult.Text = ex.Message
            Return False
        Finally
            If infile IsNot Nothing Then

                infile.Close()
            End If
        End Try
        Return False
    End Function

    Private Sub btnDetails_Click() Handles BtnDetails.Click

        Dim index As Integer = LstCooking.SelectedIndex
        If index = -1 Then
            LblResult.Text = "Please select a cooking"
            Return
        Else
            LblResult.Text = String.Empty
        End If

        Dim frm As New DetailsForm
        frm.onecooking = allcookings(index)
        frm.ShowDialog()
        FillList()
    End Sub


End Class


PS

The arrow was added just to locate the line that s raising the error message

Once again thanks for any help


Answers (18)